Skip to content

Instantly share code, notes, and snippets.

@kenechiokolo
Created June 26, 2016 18:54
Show Gist options
  • Save kenechiokolo/354c856535c2d2547c2a3433292e86fd to your computer and use it in GitHub Desktop.
Save kenechiokolo/354c856535c2d2547c2a3433292e86fd to your computer and use it in GitHub Desktop.
//: 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