Last active
January 22, 2016 23:34
-
-
Save kiarafbickers/9da42cf7884c2c53a07e to your computer and use it in GitHub Desktop.
Unique Characters in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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