Skip to content

Instantly share code, notes, and snippets.

@madson
Last active November 3, 2017 21:36
Show Gist options
  • Save madson/4343a8622b926bf2704eb0711d901ebb to your computer and use it in GitHub Desktop.
Save madson/4343a8622b926bf2704eb0711d901ebb to your computer and use it in GitHub Desktop.
import Foundation
var text = "baabc"
func removeDuplicatedCharacters(in text: String) -> String {
var other = text
var uniqueChars = [String]()
for char in other.characters {
let current = char.description
if !uniqueChars.contains(current) {
uniqueChars.append(current)
}
}
for char in uniqueChars {
let dup = "\(char)\(char)"
if other.contains(dup) {
other = other.replacingOccurrences(of: dup, with: "")
other = removeDuplicatedCharacters(in: other)
}
}
if other == "" {
other = "Empty String"
}
return other
}
print(removeDuplicatedCharacters(in: text))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment