What is the best algorithm for detecting duplicate numbers in array, the best in speed, memory and avoiving overhead. Small Array like [5,9,13,3,2,5,6,7,1] Note that 5 i dublicate.

After searching and reading about sorting algorithms, I realized that I will use one of these algorithms, Quick Sort, Insertion Sort or Merge Sort.

But actually I am really confused about what to use in my case which is a small array.

Thanks in advance.

To be honest, with that size of array, you may as well choose the `O(n`

solution (checking every element against every other element).^{2})

You'll generally only need to worry about performance if/when the array gets larger. For small data sets like this, you could well have found the duplicate with an 'inefficient' solution before the sort phase of an efficient solution will have finished :-)

In other words, you can use something like (pseudo-code):

```
for idx1 = 0 to nums.len - 2 inclusive:
for idx2 = idx1 + 1 to nums.len - 1 inclusive:
if nums[idx1] == nums[idx2]:
return nums[idx1]
return no dups found
```

This finds the first value in the array which has a duplicate.

If you want an exhaustive list of duplicates, then just add the duplicate value to another (initially empty) array (once only per value) and keep going.

You *can* sort it using *any* half-decent algorithm though, for a data set of the size you're discussing, even a bubble sort would probably be adequate. Then you just process the sorted items sequentially, looking for runs of values but it's probably overkill in your case.