Created
April 15, 2018 22:21
-
-
Save fitomad/f6aae1ae535f9f6cb9e7786038200521 to your computer and use it in GitHub Desktop.
Obtain the first word duplicated in a phrase.
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
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