【问题标题】:How do I check what is being repeated in the array? [duplicate]如何检查数组中重复的内容? [复制]
【发布时间】: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++){
}

【问题讨论】:

    标签: ios arrays swift


    【解决方案1】:
    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
    

    【讨论】:

      猜你喜欢
      • 2021-07-23
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      • 2011-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多