Unique Number


We are going to write an algorithm that will detect the String to make sure that it is unique in the sense of not containing duplicate alphabet. For example, the word Hello is not unique as it contains 2 l’s.

With the code below, we simply return either true or false depending if the value is unique or not. The code loops through the input appending each character into usedLetters  and with the if statement to check if there are duplicates. If there is duplicates, it will return false.

func isUnique(input:String) -> Bool {
    var usedLetters = [Character]()
    for letter in input {
        if usedLetters.contains(letter) {
            return false
    return true

The result is as follow

isUnique(input: "Hello")        // will return false
isUnique(input: "Whats up")     // will return true


  • Article By :
    Founder of DaddyCoding. Studied Computer Science, Information System and Information Technology at BYU-Hawaii. Currently spending most of my time researching and learning on helping to expose making iOS apps.

Random Posts

Leave a Reply

Your email address will not be published. Required fields are marked *