从基于另一个数组的数组中删除对象

时间:2022-09-25 07:54:48

I have two arrays like this:

我有两个这样的数组:

var arrayA = ["Mike", "James", "Stacey", "Steve"]
var arrayB = ["Steve", "Gemma", "James", "Lucy"]

As you can see, James and Steve match and I want to be able to remove them from arrayA. How would I write this?

如你所见,詹姆斯和史蒂夫匹配,我想把他们从阿瑞亚移走。我该怎么写呢?

8 个解决方案

#1


14  

Like this:

是这样的:

var arrayA = ["Mike", "James", "Stacey", "Steve"]
var arrayB = ["Steve", "Gemma", "James", "Lucy"]
for word in arrayB {
    if let ix = find(arrayA, word) {
        arrayA.removeAtIndex(ix)
    }
}
// now arrayA is ["Mike", "Stacey"]

#2


31  

@francesco-vadicamo's answer in Swift 2/3/4+

@francesco- vradicamo回答,用2/3/4+

 arrayA = arrayA.filter { !arrayB.contains($0) }

#3


25  

The easiest way is by using the new Set container (added in Swift 1.2 / Xcode 6.3):

最简单的方法是使用新的Set容器(在Swift 1.2 / Xcode 6.3中添加):

var setA = Set(arrayA)
var setB = Set(arrayB)

// Return a set with all values contained in both A and B
let intersection = setA.intersect(setB) 

// Return a set with all values in A which are not contained in B
let diff = setA.subtract(setB)

If you want to reassign the resulting set to arrayA, simply create a new instance using the copy constructor and assign it to arrayA:

如果您想将结果集重新分配给arrayA,只需使用copy构造函数创建一个新实例,并将其分配给arrayA:

arrayA = Array(intersection)

The downside is that you have to create 2 new data sets. Note that intersect doesn't mutate the instance it is invoked in, it just returns a new set.

缺点是您必须创建两个新的数据集。注意,intersect不会改变调用它的实例,它只返回一个新的集合。

There are similar methods to add, subtract, etc., you can take a look at them

有类似的加减方法,你可以看看

#4


10  

I agree with Antonio's answer, however for small array subtractions you can also use a filter closure like this:

我同意安东尼奥的回答,但是对于小数组减法,你也可以使用如下的过滤器闭包:

let res = arrayA.filter { !contains(arrayB, $0) }

#5


9  

matt and freytag's solutions are the ONLY ones that account for duplicates and should be receiving more +1s than the other answers.

matt和freytag的解决方案是唯一一个解释重复问题的方案,应该比其他答案接收更多的+1。

Here is an updated version of matt's answer for Swift 3.0:

以下是马特对Swift 3.0的更新版本:

var arrayA = ["Mike", "James", "Stacey", "Steve"]
var arrayB = ["Steve", "Gemma", "James", "Lucy"]
for word in arrayB {
    if let ix = arrayA.index(of: word) {
        arrayA.remove(at: ix)
    }
}

#6


5  

This can also be implemented as a minus func:

这也可以作为一个负函数来实现:

func -<T:RangeReplaceableCollectionType where T.Generator.Element:Equatable>( lhs:T, rhs:T ) -> T {

    var lhs = lhs
    for element in rhs {
        if let index = lhs.indexOf(element) { lhs.removeAtIndex(index) }
    }

    return lhs
}

Now you can use

现在你可以使用

arrayA - arrayB

#7


2  

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:

使用数组→设置→数组方法提到的安东尼奥,方便操作员,正如freytag指出的那样,我一直在使用这个非常满意:

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

#8


1  

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"]
    

#1


14  

Like this:

是这样的:

var arrayA = ["Mike", "James", "Stacey", "Steve"]
var arrayB = ["Steve", "Gemma", "James", "Lucy"]
for word in arrayB {
    if let ix = find(arrayA, word) {
        arrayA.removeAtIndex(ix)
    }
}
// now arrayA is ["Mike", "Stacey"]

#2


31  

@francesco-vadicamo's answer in Swift 2/3/4+

@francesco- vradicamo回答,用2/3/4+

 arrayA = arrayA.filter { !arrayB.contains($0) }

#3


25  

The easiest way is by using the new Set container (added in Swift 1.2 / Xcode 6.3):

最简单的方法是使用新的Set容器(在Swift 1.2 / Xcode 6.3中添加):

var setA = Set(arrayA)
var setB = Set(arrayB)

// Return a set with all values contained in both A and B
let intersection = setA.intersect(setB) 

// Return a set with all values in A which are not contained in B
let diff = setA.subtract(setB)

If you want to reassign the resulting set to arrayA, simply create a new instance using the copy constructor and assign it to arrayA:

如果您想将结果集重新分配给arrayA,只需使用copy构造函数创建一个新实例,并将其分配给arrayA:

arrayA = Array(intersection)

The downside is that you have to create 2 new data sets. Note that intersect doesn't mutate the instance it is invoked in, it just returns a new set.

缺点是您必须创建两个新的数据集。注意,intersect不会改变调用它的实例,它只返回一个新的集合。

There are similar methods to add, subtract, etc., you can take a look at them

有类似的加减方法,你可以看看

#4


10  

I agree with Antonio's answer, however for small array subtractions you can also use a filter closure like this:

我同意安东尼奥的回答,但是对于小数组减法,你也可以使用如下的过滤器闭包:

let res = arrayA.filter { !contains(arrayB, $0) }

#5


9  

matt and freytag's solutions are the ONLY ones that account for duplicates and should be receiving more +1s than the other answers.

matt和freytag的解决方案是唯一一个解释重复问题的方案,应该比其他答案接收更多的+1。

Here is an updated version of matt's answer for Swift 3.0:

以下是马特对Swift 3.0的更新版本:

var arrayA = ["Mike", "James", "Stacey", "Steve"]
var arrayB = ["Steve", "Gemma", "James", "Lucy"]
for word in arrayB {
    if let ix = arrayA.index(of: word) {
        arrayA.remove(at: ix)
    }
}

#6


5  

This can also be implemented as a minus func:

这也可以作为一个负函数来实现:

func -<T:RangeReplaceableCollectionType where T.Generator.Element:Equatable>( lhs:T, rhs:T ) -> T {

    var lhs = lhs
    for element in rhs {
        if let index = lhs.indexOf(element) { lhs.removeAtIndex(index) }
    }

    return lhs
}

Now you can use

现在你可以使用

arrayA - arrayB

#7


2  

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:

使用数组→设置→数组方法提到的安东尼奥,方便操作员,正如freytag指出的那样,我一直在使用这个非常满意:

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

#8


1  

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"]