Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- FirstNonRepeated[str_] := With[{chars = str // Characters},
- Select[
- {#, Count[chars, #]} & /@ chars,
- #[[2]] == 1 &
- ][[1, 1]]
- ]
- (* now lets test this *)
- FirstNonRepeated /@ {"teeter", "stress", "test"}
- (* will output: *)
- {"r", "t", "e"}
- (* this is how you "read", in normal language, the code above: *)
- (* create a function of a string... split the str up into its characters
- FirstNonRepeated[str_] := With[{chars = str // Characters},
- (* select the elements ... *)
- Select[
- (* ... from the list of characters with their according count of occurence ... *)
- {#, Count[chars, #]} & /@ chars,
- (* ... where the count is 1 *)
- #[[2]] == 1 &
- ][[1, 1]] (* ... from this pick the the first character *)
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement