I think the easiest way to modify your code to do that is to check whether j > i in your inner loop, i.e. if you find a number equal to arr[i], but
before it, break the inner loop and continue to the next iteration of the outer one.
Other than that, there's a couple of other options, such as
- sorting the array before counting
- remembering which numbers you already encountered
- using an associative data structure such as a
map