iOS swift从另一个数组中删除数组的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31984435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:14:45  来源:igfitidea点击:

iOS swift remove elements of an array from another array

iosarraysswift

提问by Dev Imak

I have two arrays

我有两个数组

var array1 = new Array ["a", "b", "c", "d", "e"]
var array2 = new Array ["a", "c", "d"]

I want to remove elements of array2 from array1

我想从 array1 中删除 array2 的元素

Result ["b", "e"]

回答by jrc

@Antonio's solutionis more performant, but this preserves ordering, if that's important:

@ Antonio 的解决方案性能更高,但如果这很重要,这会保留排序:

var array1 = ["a", "b", "c", "d", "e"]
let array2 = ["a", "c", "d"]
array1 = array1.filter { !array2.contains(
array1 = Array(Set(array1).subtracting(array2))
) }

回答by Antonio

The easiest way is to convert both arrays to sets, subtract the second from the first, convert to the result to an array and assign it back to array1:

最简单的方法是将两个数组转换为集合,从第一个中减去第二个,将结果转换为数组并将其分配回array1

var array1 = ["a", "b", "c", "d", "e"]
var array2 = ["a", "c", "d"]

Note that your code is not valid Swift - you can use type inference to declare and initialize both arrays as follows:

请注意,您的代码不是有效的 Swift - 您可以使用类型推断来声明和初始化两个数组,如下所示:

let animals = ["cats", "dogs", "chimps", "moose", "squarrel", "cow"]
let indexAnimals = [0, 3, 4]
let arrayRemainingAnimals = animals
    .enumerated()
    .filter { !indexAnimals.contains(
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(
let animals = ["cats", "dogs", "chimps", "moose", "squarrel", "cow"]
let indexAnimals = [0, 3, 4]
let arrayRemainingAnimals = animals
    .enumerated()
    .filter { !indexAnimals.contains(
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(
let arrayResult = numbers.filter { element in
    return !indexesToRemove.contains(element)
}
print(arrayResult)

//result - [0, 1, 2, 4, 6, 7, 9, 10, 11]
.offset) } .map {
let arrayLetters = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
let arrayRemoveLetters = ["a", "e", "g", "h"]
let arrayRemainingLetters = arrayLetters.filter {
    !arrayRemoveLetters.contains(
let arrayResult = numbers.filter { element in
    return !indexesToRemove.contains(element)
}
print(arrayResult)

//result - [0, 1, 2, 4, 6, 7, 9, 10, 11]
) } print(arrayRemainingLetters) //result - ["b", "c", "d", "f", "i"]
.element } print(numbers) //result - [0, 1, 2, 4, 6, 7, 9, 10, 11]
.offset) } .map {
let arrayLetters = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
let arrayRemoveLetters = ["a", "e", "g", "h"]
let arrayRemainingLetters = arrayLetters.filter {
    !arrayRemoveLetters.contains(
extension Array where Element: Equatable {
    func subtracting(_ array: Array<Element>) -> Array<Element> {
        self.filter { !array.contains(
let setA = Set(arr1)
let setB = Set(arr2)
setA.subtract(setB)
) } } }
) } print(arrayRemainingLetters) //result - ["b", "c", "d", "f", "i"]
.element } print(arrayRemainingAnimals) //result - ["dogs", "chimps", "cow"]
.offset) } .map {
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"not (self IN %@)", subArrayToBeDeleted];
NSArray* finalArray = [initialArray filteredArrayUsingPredicate:predicate];
.element } print(numbers) //result - [0, 1, 2, 4, 6, 7, 9, 10, 11]
.offset) } .map { ##代码##.element } print(arrayRemainingAnimals) //result - ["dogs", "chimps", "cow"]

回答by Krunal

Remove elements using indexes array:

使用索引数组删除元素:

  1. Array of Strings and indexes

    ##代码##
  2. Array of Integers and indexes

    ##代码##
  1. 字符串和索引数组

    ##代码##
  2. 整数和索引数组

    ##代码##



Remove elements using element value of another array



使用另一个数组的元素值删除元素

  1. Arrays of integers

    ##代码##
  2. Arrays of strings

    ##代码##
  1. 整数数组

    ##代码##
  2. 字符串数组

    ##代码##

回答by Shai Balassiano

Here is an extension with @jrc answer:

这是@jrc 答案的扩展:

##代码##

回答by Adnan Aftab

You can create sets and then use the subtract method

您可以创建集合,然后使用减法方法

##代码##

回答by Tung Fam

out of scope but it would help me if it had been here. removing subArray from array in OBJECTIVE-C

超出范围,但如果它在这里,它会帮助我。从 OBJECTIVE -C 中的数组中删除 subArray

##代码##

hope it will ever help someone :)

希望它会帮助某人:)