Let\'s say there are two arrays...
var array1 = [\"a\", \"b\", \"c\"]
var array2 = [\"b\", \"c\", \"a\"]
I\'d like the result of the compar
Solution for Swift 4.1/Xcode 9.4:
extension Array where Element: Equatable {
func containSameElements(_ array: [Element]) -> Bool {
var selfCopy = self
var secondArrayCopy = array
while let currentItem = selfCopy.popLast() {
if let indexOfCurrentItem = secondArrayCopy.index(of: currentItem) {
secondArrayCopy.remove(at: indexOfCurrentItem)
} else {
return false
}
}
return secondArrayCopy.isEmpty
}
}
The main advantage of this solution is that it uses less memory than other (it always creates just 2 temporary arrays). Also, it does not require for Element to be Comparable, just to be Equatable.