This question is similliar to my other one
I have 2 arrays:
fruitsArray = ["apple", "mango", "blueberry", "orange"]
vegArray = ["tomato", "potato", "mango", "blueberry"]
How can I get the uncommon elements of fruitsArray
comparing to vegArray
Expected Output = ["apple", orange]
To check if two arrays are equal in Swift, use Equal To == operator. Equal To operator returns a Boolean value indicating whether two arrays contain the same elements in the same order. If two arrays are equal, then the operator returns true , or else it returns false .
The contains() method returns: true - if the array contains the specified element. false - if the array doesn't contain the specified element.
Convert arrays to Set and use subtract() function to get what you want
let fruitsArray = ["apple", "mango", "blueberry", "orange"]
let vegArray = ["tomato", "potato", "mango", "blueberry"]
let set1 = Set(fruitsArray)
let set2 = Set(vegArray)
let filter = Array(set1.subtract(set2))
println(filter) //[apple, orange]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With