Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.