Determine if a string has all unique characters: Difference between revisions
Determine if a string has all unique characters (view source)
Revision as of 21:49, 30 December 2019
, 4 years ago→{{header|Haskell}}
Line 530:
(zip [0 ..] xs))))
-- OR, fusing filter, toList, and minimumByMay down to a single fold:
duplicatedCharIndices_ :: String -> Maybe (Char, [Int])
duplicatedCharIndices_ xs =
go
Nothing
go k xs@(x:_) (Just (c, ys@(y:_))) =▼
(foldl'
(\a (i, c)
(zip [0 ..] xs))
▲ in M.foldrWithKey
go k [_]
go k xs Nothing = Just (
go k
if x <
then Just (
---------------------------TEST----------------------------
Line 564 ⟶ 566:
duplicatedCharIndices_
["", ".", "abcABC", "XYZ ZYX", "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ"]
--------------------------DISPLAY--------------------------
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
fTable s xShow fxShow f xs =
s : fmap (((++)
where
▲ in unlines $
rjust
w = maximum (length . xShow <$> xs)</lang>
{{Out}}
<pre>First duplicated character, if any:
|