Determine if a string has all unique characters: Difference between revisions

→‎{{header|Haskell}}: Updated the variant in terms of find
(→‎{{header|Haskell}}: Added a Haskell variant.)
(→‎{{header|Haskell}}: Updated the variant in terms of find)
Line 444:
 
Or perhaps, as an alternative to grouping, sorting, and zipping:
<lang haskell>import Data.List (findIndicesfind, intercalate)
import Numeric (showHex)
import Data.Bool (bool)
Line 450:
 
duplicatedCharIndices :: String -> Maybe (Char, [Int])
duplicatedCharIndices xs = go (zip xs [0 ..])
where
go [] = Nothing
go [_] = Nothing
go (c:cs) = bool (go cs) (Just ((c, findIndices (c ==i) :xs))) (elem c cs)=
maybe (go xs) (\(x, j) -> Just (x, [i, j])) (find ((c ==) . fst) xs)
 
---------------------------TEST----------------------------
Line 475 ⟶ 477:
duplicatedCharIndices
["", ".", "abcABC", "XYZ ZYX", "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ"]
 
--------------------------DISPLAY--------------------------
9,659

edits