Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 Remove objects from an array that are present in another array using set and maintaining order [duplicate]

Tags:

arrays

swift

Array1 = [1, 2, 3, 4, 5, 6]

Array2 = [1,5]

I want to get:

Array1 = [2, 3, 4, 6]

I want to do this by using Set because these arrays may get larger. Also it is important that I maintain the order of the array.

like image 879
user6520705 Avatar asked Mar 24 '17 15:03

user6520705


3 Answers

Create a set with all elements from the second array, then filter the first array to get only the elements which are not in the set:

let array1 = [5, 4, 1, 2, 3, 4, 1, 2]
let array2 = [1, 5]

let set2 = Set(array2)
let result = array1.filter { !set2.contains($0) }

print(result) // [4, 2, 3, 4, 2]

This preserves the order (and duplicate elements) from the first array. Using a set is advantageous if the second array can be large, because the lookup is faster.

like image 133
Martin R Avatar answered Oct 17 '22 13:10

Martin R


var array1 = [1, 2, 3, 4, 5, 6]

var array2 = [1,5]

var arrayResult = array1.enumerated()
    .filter { !array2.contains($0.0 + 1) }
    .map { $0.1 }

print(arrayResult)

[2, 3, 4, 6]


Another ways to achieve the same result:

1. User filter

let arrayResult = array1.filter { element in
    return !array2.contains(element)
}

2. Use Sort

array2.sorted(by: >).forEach { if $0 < self.array1.count { self.array1.remove(at: $0) } }  



Remove elements using indexes array:

  1. Array of Strings and indexes

    let animals = ["cats", "dogs", "chimps", "moose", "squarrel", "cow"]
    let indexAnimals = [0, 3, 4]
    let arrayRemainingAnimals = animals
        .enumerated()
        .filter { !indexAnimals.contains($0.offset) }
        .map { $0.element }
    
    print(arrayRemainingAnimals)
    
    //result - ["dogs", "chimps", "cow"]
    
  2. Array of Integers and indexes

    var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    let indexesToRemove = [3, 5, 8, 12]
    
    numbers = numbers
        .enumerated()
        .filter { !indexesToRemove.contains($0.offset) }
        .map { $0.element }
    
    print(numbers)
    
    //result - [0, 1, 2, 4, 6, 7, 9, 10, 11]
    



Remove elements using element value of another array

  1. Arrays of integers

    let arrayResult = numbers.filter { element in
        return !indexesToRemove.contains(element)
    }
    print(arrayResult)
    
    //result - [0, 1, 2, 4, 6, 7, 9, 10, 11]
    
  2. Arrays of strings

    let arrayLetters = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
    let arrayRemoveLetters = ["a", "e", "g", "h"]
    let arrayRemainingLetters = arrayLetters.filter {
        !arrayRemoveLetters.contains($0)
    }
    
    print(arrayRemainingLetters)
    
    //result - ["b", "c", "d", "f", "i"]
    
like image 36
Krunal Avatar answered Oct 17 '22 14:10

Krunal


Use the filter function

let result = Array1.filter { element in
    return !Array2.contains(element)
}
like image 1
Peter Tao Avatar answered Oct 17 '22 13:10

Peter Tao