0

I have an array with 1 million objects and it contains 1 duplicate? What is the most efficient way to identify the duplicate and remove it?

Pawel Gumiela
  • 1,992
  • 2
  • 20
  • 36
syed k
  • 9
  • 1

1 Answers1

0
  1. If it is sorted, just go from left to right until you find two of them in row

    Average complexity : O(n/2)

    Worst complexity : O(n)

  2. If it is not sorted and you have enough memory, create hashmap, store objects as keys and when you hit one key twice, it is duplicate.

    Average complexity : O(n/2)

    Worst complexity : O(n)

  3. If it is not sorted and you do not have enough memory, use quicksort to sort it and then use approach no. 1

    Average complexity : O(n*log n)

    Worst complexity : O(n^2)

libik
  • 22,239
  • 9
  • 44
  • 87