Arrays 在ios swift中获取副本

Arrays 在ios swift中获取副本,arrays,swift,filter,Arrays,Swift,Filter,我有一个数组,数组中有2000个字典,我想找出数组中的重复项 let contacts = [ Contact(name: "siddhant", phone: "1234"), Contact(name: "nitesh", phone: "1234"), Contact(name: "nitin", phone: "2222"), Contact(name: "himanshu", phone: "2222"), Contact(name

我有一个数组,数组中有2000个字典,我想找出数组中的重复项

let contacts = [
    Contact(name: "siddhant",     phone: "1234"),
    Contact(name: "nitesh", phone: "1234"),
    Contact(name: "nitin",  phone: "2222"),
    Contact(name: "himanshu",   phone: "2222"),
    Contact(name: "kedar",    phone: "3333")
]
//输出应为:

[
        Contact(name: "siddhant",     phone: "1234"),
        Contact(name: "nitesh", phone: "1234"),
        Contact(name: "nitin",  phone: "2222"),
        Contact(name: "himanshu",   phone: "2222")
    ]
以下是我尝试过的:-

let result = values.filter { value in values.filter({ $0.phone == value.phone }).count > 1 } 
print(result) //this takes lot time to filter 2000+ datas

按电话号码分组,消除所有不重复的电话号码,然后过滤原始阵列:

struct Contact {
    let name:String
    let phone:String
}
let contacts = [
    Contact(name: "siddhant", phone: "1234"),
    Contact(name: "nitesh", phone: "1234"),
    Contact(name: "nitin",  phone: "2222"),
    Contact(name: "himanshu", phone: "2222"),
    Contact(name: "kedar", phone: "3333")
]
let singlePhones = Set(Dictionary(grouping: contacts, by: {$0.phone}).filter{$0.1.count == 1}.map{$0.0})
let contacts2 = contacts.filter {!singlePhones.contains($0.phone)}
结果似乎是你所要求的:

[Contact(name: "siddhant", phone: "1234"),
Contact(name: "nitesh", phone: "1234"), 
Contact(name: "nitin", phone: "2222"), 
Contact(name: "himanshu", phone: "2222")]


让result=values.filter{value in values.filter{$0.phone==value.phone}.count>1}printresult这需要很多时间来过滤2000多个数据可能重复的我不明白你在问什么:应该过滤行Contactname:kedar,phone:3333,但是如何过滤,在你的示例中它不是任何其他联系人的重复。我认为您的代码是递归的,因此需要很多时间,但这不是必需的。见上面Sahil Machanda的评论。非常感谢这对我很有帮助,但我能知道如何获得[联系人姓名:siddhant,电话:1234,联系人姓名:nitin,电话:2222]