Skip to content

Instantly share code, notes, and snippets.

@fitomad
Created April 15, 2018 22:21
Show Gist options
  • Save fitomad/f6aae1ae535f9f6cb9e7786038200521 to your computer and use it in GitHub Desktop.
Save fitomad/f6aae1ae535f9f6cb9e7786038200521 to your computer and use it in GitHub Desktop.
Obtain the first word duplicated in a phrase.
let phrase: String = "If you know what you know if I..."
let words = phrase.split(separator: " ")
/// Check if a given word is includen in a string
/// before the own word.
func containsWord(_ word: String, offsetLimit limit: Int) -> Bool
{
return words.enumerated()
.filter({ $0.offset < limit })
.map({ String($0.element) })
.contains(word)
}
for (index, word) in words.enumerated()
{
if containsWord(String(word), offsetLimit: index)
{
print("The first word duplicated is \(word)")
break
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment