Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func removeDupLetters(from text: String) -> String {
- //Turn each letter in (Int,Char), assigning a unique number to every letter
- let text = text.enumerated()
- //Gouping text by letter into a dictionary; loses order
- let textDict = Dictionary(grouping: text, by: {$0.element})
- //Returning the first value(Int,Char) of every key(letter), and sorting incremently by the enumerated value
- let singles = textDict.compactMap({$0.value.first}).sorted(by: {$0.offset < $1.offset})
- textDict.forEach({print($0)})
- print("")
- singles.forEach({print($0)})
- print("------")
- return String(singles.compactMap({$0.element}))
- }
- removeDupLetters(from: "hello")
- removeDupLetters(from: "wombat")
- removeDupLetters(from: "Mississippi")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement