This function returns the number of pairs of the same elements in the array
It takes, array length and the array as the parameter
n: Length of array
ar: Array of integers for example
4 6 7 8 7 6 6 7 6 4
returns 4
fun getNumberOfPairs(n: Int, ar: Array<Int>): Int {
val enteries = HashSet<Int>()
var pairs = 0
for (i in 0 until n) {
if (!enteries.contains(ar[i])) {
enteries.add(ar[i])
} else {
pairs++
enteries.remove(ar[i])
}
}
println(pairs)
return pairs
}
How can we write this code in a better way for readability/performance?