有没有简单的方法可以在swift中合并两个数组并删除重复项?

时间:2021-03-17 12:14:16

Basically I need a version of appendContentsOf: which does not append duplicate elements.

基本上我需要一个appendContentsOf的版本:它不附加重复的元素。

Example

var a = [1, 2, 3]
let b = [3, 4, 5]

a.mergeElements(b)
//gives a = [1, 2, 3, 4, 5] //order does not matter

6 个解决方案

#1


10  

Simply :

简单地说

let unique = Array(Set(a + b))

#2


4  

This is commonly called a union, which is possible in Swift using a Set:

这通常称为union,在Swift中可以使用Set:

let a = [1, 2, 3]
let b = [3, 4, 5]

let set = Set(a)
let union = set.union(b)

Then you can just convert the set into an array:

然后你可以将集合转换为数组:

let result = Array(union)

#3


2  

Swift 3.0 version of the accepted answer.

Swift 3.0版本接受的答案。

extension Array where Element : Equatable{

  public mutating func mergeElements<C : Collection>(newElements: C) where C.Generator.Element == Element{
    let filteredList = newElements.filter({!self.contains($0)})
    self.append(contentsOf: filteredList)
  }

}

Note: Worth saying here that the array passed to the function is the array of object that will be omitted from the final array. Important if your merging an array of objects where the Equatable property may be the same but others may differ.

注意:值得一提的是,传递给函数的数组是最终数组中将省略的对象数组。重要的是,如果合并一个对象数组,其中Equatable属性可能相同但其他属性可能不同。

#4


1  

An Array extension can be created to do this.

可以创建一个Array扩展来执行此操作。

extension Array where Element : Equatable{

    public mutating func mergeElements<C : CollectionType where C.Generator.Element == Element>(newElements: C){
       let filteredList = newElements.filter({!self.contains($0)})
       self.appendContentsOf(filteredList)
   }
}

Of course, this is useful for only Equatable elements.

当然,这仅适用于Equatable元素。

#5


1  

Swift 4.0 Version

Swift 4.0版

extension Array where Element : Equatable {

  public mutating func mergeElements<C : Collection>(newElements: C) where C.Iterator.Element == Element{
    let filteredList = newElements.filter({!self.contains($0)})
    self.append(contentsOf: filteredList)
  }

}

As mentioned: The array passed to the function is the array of object that will be omitted from the final array

如上所述:传递给函数的数组是将从最终数组中省略的对象数组

#6


0  

Swift 4

斯威夫特4

func combine(_ sets: Set<String>?...) -> Set<String> {
    return sets.compactMap{$0}.reduce(Set<String>()){$0.union($1)}
}

#1


10  

Simply :

简单地说

let unique = Array(Set(a + b))

#2


4  

This is commonly called a union, which is possible in Swift using a Set:

这通常称为union,在Swift中可以使用Set:

let a = [1, 2, 3]
let b = [3, 4, 5]

let set = Set(a)
let union = set.union(b)

Then you can just convert the set into an array:

然后你可以将集合转换为数组:

let result = Array(union)

#3


2  

Swift 3.0 version of the accepted answer.

Swift 3.0版本接受的答案。

extension Array where Element : Equatable{

  public mutating func mergeElements<C : Collection>(newElements: C) where C.Generator.Element == Element{
    let filteredList = newElements.filter({!self.contains($0)})
    self.append(contentsOf: filteredList)
  }

}

Note: Worth saying here that the array passed to the function is the array of object that will be omitted from the final array. Important if your merging an array of objects where the Equatable property may be the same but others may differ.

注意:值得一提的是,传递给函数的数组是最终数组中将省略的对象数组。重要的是,如果合并一个对象数组,其中Equatable属性可能相同但其他属性可能不同。

#4


1  

An Array extension can be created to do this.

可以创建一个Array扩展来执行此操作。

extension Array where Element : Equatable{

    public mutating func mergeElements<C : CollectionType where C.Generator.Element == Element>(newElements: C){
       let filteredList = newElements.filter({!self.contains($0)})
       self.appendContentsOf(filteredList)
   }
}

Of course, this is useful for only Equatable elements.

当然,这仅适用于Equatable元素。

#5


1  

Swift 4.0 Version

Swift 4.0版

extension Array where Element : Equatable {

  public mutating func mergeElements<C : Collection>(newElements: C) where C.Iterator.Element == Element{
    let filteredList = newElements.filter({!self.contains($0)})
    self.append(contentsOf: filteredList)
  }

}

As mentioned: The array passed to the function is the array of object that will be omitted from the final array

如上所述:传递给函数的数组是将从最终数组中省略的对象数组

#6


0  

Swift 4

斯威夫特4

func combine(_ sets: Set<String>?...) -> Set<String> {
    return sets.compactMap{$0}.reduce(Set<String>()){$0.union($1)}
}