I have used one more array to store the print value.We can use set as well as.I hope you get the answer.
let array = [1,2,1,3,4,5,6,2]
var arrayWithPosition = [Int]()
for i in 0..<array.count {
var position = 0
while position < arrayWithPosition.count {
if arrayWithPosition[position] == array[i]{
break
}
position += 1
}
if position == arrayWithPosition.count{
arrayWithPosition.append(array[i])
}else{
continue
}
var count = 1
for j in i+1..<array.count {
if array[i] == array[j] {
count += 1
}
}
print(array[i],count)
}
Output : -
1 2
2 2
3 1
4 1
5 1
6 1
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…