I have a simple line of code with 2 String Arrays. They both contain the same strings inside both arrays and I have an if statement that will work if both arrays are the same. Like so:
var firstArray: [String] = ["Music", "Art", "Sports", "Movies"]
var secondArray: [String] = ["Music", "Art", "Sports", "Movies"]
if firstArray == secondArray {
//they match...
}
You can find the size of the intersection between the two arrays:
let firstArray = ["Music", "Art", "Sports", "Movies"]
let secondArray = ["Music", "Art", "Sports", "Movies"]
if firstArray == secondArray {
print("equal")
}
if Set(firstArray).intersection(secondArray).count >= 4 {
print("At least 4 are equal")
}
Keep in mind that converting an Array
to a Set
is a O(n)
operation. If you're doing this often, cache the Set
rather than recomputing it on every check.