I have a sorted array. I would like to iterate through the array and increment a counter as I find pairs of values. I'm not finding an elegant solution to this.
var pairs = 0
let colors = [10, 20, 20, 10, 10, 30, 50, 10, 20
let sortedColors = colors.sorted{ $0 < $1}
// [10, 10, 10, 10, 20, 20, 20, 30, 50] -> pairs should equal 3
for i in 0..<colors.count - 1 {
if sortedColors[i+1] != colors.count && sortedColors[i] == sortedColors[i+1] {
pairs += 1
}
}
print(pairs)