Copying the array, sorting with a merge sort (or quicksort for primitives) and searching for duplicates will run in O(
n log
n) time, whereas repeated linear searches are more-or-less equivalent to bubble sort, which runs in quadratic time. So I like Rob's suggestion.
Another way to do it is to design a binary tree and use it as a sorted set. Add all the elements in the array to it. Get it to return
true or
false depending whether the element has been added; returning
false suggests you have hit a duplicate. That will also run in O(
n log
n) time. Or you can look at the
Map pages in the Java™ Tutorials; there is an example about counting words, which you can adapt to the present problem. Anything which returns a count of 2 is a duplicate, triplicates will return 3, etc. Adding to a Map runs in amortised constant time, so the whole procedure will run in linear time.
All these suggestions depend on the elements not changing their state during the counting process, otherwise they will be counted wrongly, or (in a Map) will disappear mysteriously, never to be found again. (Unless they change their state back, when they will just as mysteriously reappear.)