How can I remove values array based on array other.
Example :-
Type ( Int )
var arrayOne = [1,2,3,4,5,6,7,8]
var arrayTwo = [1,2,4,5,6,7]
I want the next result :-
result = [3,8]
How can I remove values array based on array other.
Example :-
Type ( Int )
var arrayOne = [1,2,3,4,5,6,7,8]
var arrayTwo = [1,2,4,5,6,7]
I want the next result :-
result = [3,8]
User Set for simple result
var arrayOne = [1,2,3,4,5,6,7,8]
var arrayTwo = [1,2,4,5,6,7]
let set1:Set<Int> = Set(arrayOne)
let set2:Set<Int> = Set(arrayTwo)
let set3 = set1.symmetricDifference(set2)
Output :
{3, 8}
You can do it by converting them to Sets.
var arrayOne = [1,2,3,4,5,6,7,8]
var arrayTwo = [1,2,4,5,6,7]
let set1:Set<String> = Set(arrayOne)
let set2:Set<String> = Set(arrayTwo)
Use ExclusiveOr to do this.
//Swift 2.0
set1.exclusiveOr(array2) // result = {3,8}
//Swift 3.0
set1.symmetricDifference(set2) // result = {3,8}
This link might useful for you: Set operations (union, intersection) on Swift array? also possible Duplicate as @user3589771 said.
You can do with below options.
// Option 1 - Using Set's
let arrayOne : Set = [1,2,3,4,5,6,7,8]
let arrayTwo : Set = [1,2,4,5,6,7]
var result = arrayOne.symmetricDifference(arrayTwo)
print(result) // {3,8}
// Option 2 - Using Array
var result1 = [Int]()
for value in arrayOne
{
if !arrayTwo.contains(value)
{
result1.append(value)
}
}
print(result1) // {3,8}