gpt4 book ai didi

ios - Swift - 在列表中查找重复元素

转载 作者:搜寻专家 更新时间:2023-10-30 23:09:47 24 4
gpt4 key购买 nike

我有以下 UITextField 列表:

let list = [(name1TextField, phone1TextField), (name2TextField, phone2TextField), (name3TextField, phone3TextField), (name4TextField, phone4TextField), (name5TextField, phone5TextField)]

我正在尝试查找手机副本并将其打印出来

编辑

例如(元组可以为空)

list = [("john", "555-444-333"), ("james", "555-444-333"), ("",""), ("bob", "333-222-111"), ("nancy", "222-111-444"), ]

output 555-444-333

我该怎么办?

最佳答案

鉴于此

var name1TextField: UITextField!
var phone1TextField: UITextField!
var name2TextField: UITextField!
var phone2TextField: UITextField!
var name3TextField: UITextField!
var phone3TextField: UITextField!
var name4TextField: UITextField!
var phone4TextField: UITextField!
var name5TextField: UITextField!
var phone5TextField: UITextField!

还有这个

let list = [(name1TextField, phone1TextField), (name2TextField, phone2TextField), (name3TextField, phone3TextField), (name4TextField, phone4TextField), (name5TextField, phone5TextField)]

解决方案

let repeatedPhones = list
.flatMap { $0.1?.text }
.reduce([String:Int]()) { (var dict, phone) -> [String:Int] in
dict[phone] = (dict[phone] ?? 0) + 1
return dict
}
.filter { $0.1 > 1 && !$0.0.isEmpty }
.map { $0.0 }

关于ios - Swift - 在列表中查找重复元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35414715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com