【发布时间】:2016-03-03 05:07:41
【问题描述】:
我需要知道数组中重复的内容,但我不确定要使用什么方法。 [1,2,6,8,8,8,2]。我需要它来打印8 occurs 3 times2 occurs 2 times。我该怎么做。
for(var i = 0; i < numberarray.count; i++){
}
【问题讨论】:
我需要知道数组中重复的内容,但我不确定要使用什么方法。 [1,2,6,8,8,8,2]。我需要它来打印8 occurs 3 times2 occurs 2 times。我该怎么做。
for(var i = 0; i < numberarray.count; i++){
}
【问题讨论】:
let numbers = [1,2,6,8,8,8,2]
let cs = NSCountedSet(array: numbers)
cs.forEach { if cs.countForObject($0) > 1 { print("number: \($0) has an occurance of \(cs.countForObject($0))") } }
这样的?
输出:
number: 2 has an occurance of 2
number: 8 has an occurance of 3
【讨论】: