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

cu6pst1q  于 2023-01-14  发布在  iOS
关注(0)|答案(9)|浏览(310)

我有两个数组

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

我想从array1中删除array2的元素

Result ["b", "e"]
ftf50wuq

ftf50wuq1#

@Antonio's solution的性能更高,但如果需要的话,它会保留顺序:

var array1 = ["a", "b", "c", "d", "e"]
let array2 = ["a", "c", "d"]
array1 = array1.filter { !array2.contains($0) }
xdnvmnnf

xdnvmnnf2#

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

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

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

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

ovfsdjhp3#

使用索引数组删除元素:

1.字符串和索引数组

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

1.整数和索引数组

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]

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

1.整数数组

let arrayResult = numbers.filter { element in
    return !indexesToRemove.contains(element)
}
print(arrayResult)

//result - [0, 1, 2, 4, 6, 7, 9, 10, 11]

1.字符串数组

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

iszxjhcz4#

下面是一个带有@jrc的扩展名:

extension Array where Element: Equatable {
    func subtracting(_ array: Array<Element>) -> Array<Element> {
        self.filter { !array.contains($0) }
    }
}
z0qdvdin

z0qdvdin5#

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

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"not (self IN %@)", subArrayToBeDeleted];
NSArray* finalArray = [initialArray filteredArrayUsingPredicate:predicate];

希望它能帮助到别人:)

lmvvr0a8

lmvvr0a86#

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

let setA = Set(arr1)
let setB = Set(arr2)
setA.subtract(setB)
lyfkaqu1

lyfkaqu17#

Shai Balassiano的回答的延伸版本:

extension Array where Element: Equatable {
  func subtracting(_ array: [Element]) -> [Element] {
      self.filter { !array.contains($0) }
  }

  mutating func remove(_ array: [Element]) {
      self = self.subtracting(array)
  }
}
bejyjqdl

bejyjqdl8#

这里还有一个解决方案,就是定义自己的PredicateSet。

struct PredicateSet<A> {
  let contains: (A) -> Bool
}

let animals = ["Cow", "Bulldog", "Labrador"]
let dogs = ["Bulldog", "Labrador"]

let notDogs = PredicateSet { !dogs.contains($0) }

print(animals.filter(notDogs.contains)) // ["Cow"]
2jcobegt

2jcobegt9#

如果有2个数组包含重复的元素,你只需要一个简单的减法,你可以使用这段代码。这也适用于String类型。

extension Array where Element: Equatable {
    func subtracting(_ array: Array<Element>) -> Array<Element> {
        var result: Array<Element> = []
        var toSub = array
        
        for i in self {
            if let index = toSub.firstIndex(of: i) {
                toSub.remove(at: index)
                continue
            }
            else {
                result.append(i)
            }
        }
        return result
    }
}

let first = [1, 1, 2, 3, 3, 5, 6, 7, 7]
let second = [2, 2, 3, 4, 4, 5, 5, 6]
let result = first.subtracting(second)

//print result 
//[1, 1, 3, 7, 7]

相关问题