Created
June 26, 2016 18:54
-
-
Save kenechiokolo/354c856535c2d2547c2a3433292e86fd to your computer and use it in GitHub Desktop.
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
//: Playground - noun: a place where people can play | |
func uniqueString(name: String, list: [String]) -> String { | |
var suffix = 0 | |
func nameCheck(n: String) -> String { | |
var localName = n | |
guard list.contains(localName) else { return n } | |
suffix += 1 | |
localName = name + "-\(suffix)" | |
return nameCheck(n: localName) | |
} | |
return nameCheck(n: name) | |
} | |
let list = ["James", "James-1", "Kenechi", "Crystal", "Ifeoma", "Nkechi"] | |
let name = "James" | |
uniqueString(name: name, list: list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment