This question already has answers here:
Count the occurences a particular integer has in an array [duplicate]
                                
                                    (2个答案)
                                
                        
                                3年前关闭。
            
                    
我需要知道数组中正在重复的内容,而且我不确定要使用哪种方法。 [1,2,6,8,8,8,2]。我需要它来打印8 occurs 3 times 2 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

关于ios - 如何检查阵列中重复的内容? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35763641/

10-12 03:52