-
-
Save liesen/1156886 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import qualified Data.Map as M | |
import Data.List (find) | |
import Data.Maybe (fromMaybe) | |
firstNonRep :: String -> Maybe Char | |
firstNonRep xs = let m = M.fromListWith (+) . map (\c -> (c,1)) $ xs | |
lookup = flip (M.findWithDefault 0) m | |
in find ((== 1) . lookup) xs | |
main = print $ case firstNonRep "faabcbcdee" of | |
Nothing -> "no character was found just once" | |
Just c -> c : " was the first character found just once" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A more efficient solution, using the same idea as your solution: