Skip to content

Instantly share code, notes, and snippets.

@deathlezz
Last active July 31, 2021 10:56
Show Gist options
  • Save deathlezz/7576eb97a9f17bb9bab2daea6a2df119 to your computer and use it in GitHub Desktop.
Save deathlezz/7576eb97a9f17bb9bab2daea6a2df119 to your computer and use it in GitHub Desktop.
Compare elements in one array in Swift 5.
//
// Compare elements in one array
//
// Check if all elements in array are the same
let input = "aaaa"
let areSame = input.allSatisfy { $0 == input.last }
if areSame == false {
print("gooood")
} else {
print("bad input")
}
// Check the first and the last element
let input2 = " aaaa "
if input2.hasPrefix(" ") || input2.hasSuffix(" ") {
print("bad input")
} else {
print("gooood")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment