0

I need to know what is being repeated in an array, and I am not sure what method to use to do so. [1,2,6,8,8,8,2]. I need it to print 8 occurs 3 times 2 occurs 2 times. How would I do this.

for(var i = 0; i < numberarray.count; i++){
}
stackerleet
  • 518
  • 1
  • 5
  • 16

1 Answers1

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))") } }

Something like this?

Output:

number: 2 has an occurance of 2
number: 8 has an occurance of 3
Eendje
  • 8,815
  • 1
  • 29
  • 31