Skip to content

Instantly share code, notes, and snippets.

@kiarafbickers
Last active January 22, 2016 23:34
Show Gist options
  • Save kiarafbickers/9da42cf7884c2c53a07e to your computer and use it in GitHub Desktop.
Save kiarafbickers/9da42cf7884c2c53a07e to your computer and use it in GitHub Desktop.
Unique Characters in Swift
let string : String = "Hello, World!"
let uniqueString : String = "He1lo, W0r|d!"
func uniquecharacter(input: String) -> Bool
{
var isUnique: Bool = true;
let characters = Array(input.characters)
var sortedCharacters = characters.sort()
for var i: Int = 0; i < sortedCharacters.count; i++
{
let character = sortedCharacters[i]
sortedCharacters.removeAtIndex(i)
for var y : Int = 0; y < sortedCharacters.count; y++
{
let anotherCharacter = sortedCharacters[y]
if character == anotherCharacter {
print(character)
print(anotherCharacter)
isUnique = false;
return isUnique;
}
else {
sortedCharacters.removeAtIndex(i)
}
}
}
return isUnique
}
uniquecharacter(string); // false
uniquecharacter(uniqueString); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment