Advertisement
Guest User

first occurence of non repeated character

a guest
Jan 2nd, 2014
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. FirstNonRepeated[str_] := With[{chars = str // Characters},
  2. Select[
  3. {#, Count[chars, #]} & /@ chars,
  4. #[[2]] == 1 &
  5. ][[1, 1]]
  6. ]
  7.  
  8. (* now lets test this *)
  9. FirstNonRepeated /@ {"teeter", "stress", "test"}
  10.  
  11. (* will output: *)
  12. {"r", "t", "e"}
  13.  
  14.  
  15. (* this is how you "read", in normal language, the code above: *)
  16.  
  17. (* create a function of a string... split the str up into its characters
  18. FirstNonRepeated[str_] := With[{chars = str // Characters},
  19. (* select the elements ... *)
  20. Select[
  21. (* ... from the list of characters with their according count of occurence ... *)
  22. {#, Count[chars, #]} & /@ chars,
  23. (* ... where the count is 1 *)
  24. #[[2]] == 1 &
  25. ][[1, 1]] (* ... from this pick the the first character *)
  26. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement