6
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.

4
  • stackoverflow.com/a/42679608/1930509 Commented Mar 24, 2017 at 15:47
  • I can not understand exactly how to implement that answer to my arrays can you show how Commented Mar 24, 2017 at 15:48
  • see answer below Commented Mar 24, 2017 at 15:50
  • 1
    note: use lowercased varialble names to avoid confusion with Class names Commented Mar 24, 2017 at 15:51

5 Answers 5

22

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.

Sign up to request clarification or add additional context in comments.

1 Comment

OutStanding Working for me
3
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"]
    

3 Comments

This works, but I honestly think using a dictionary is a cleaner method. Would you agree with me? @Krunal
Yes, agree with you
please: use lowercased variabel names to distinct from Class names
1

Use the filter function

let result = Array1.filter { element in
    return !Array2.contains(element)
}

Comments

0

(Note: because you added to your question and maintaining order then my answer is not right anymore, because Set don't preserve the order. then the filter answers are a better fit)

use subtracting from a Set:

array1 = Array(Set(array1).subtracting(Set(array2)))

you can add this as an operator :

Using the Array → Set → Array method mentioned by Antonio, and with the convenience of an operator, as freytag pointed out, I've been very satisfied using this:

// Swift 3.x

func - <Element: Hashable>(lhs: [Element], rhs: [Element]) -> [Element]
{
    return Array(Set<Element>(lhs).subtracting(Set<Element>(rhs)))
}

quoted from: https://stackoverflow.com/a/42679608/1930509

3 Comments

can you show how you would use the operator and subtracting together I am still a little confused
Sets are unordered collections. This isn't guaranteed to maintain the order of the original items.
thx, yes i know - but he changed his original question later and added and maintaining order. i added a note to my answer.
0
let array1 = [1, 2, 3, 4, 5, 6]
let array2 = [1,5]

let array3 = array1.reduce([]) { array2.contains($1) ? $0 : $0 + [$1] } 

print(array3)  // "[2, 3, 4, 6]\n"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.