Algo:
- Solution 1:
- Have a hash table
- Iterate through array and store its elements in hash table
- As soon as you find an element which is already in hash table, it is the dup element
- Pros:
- It runs in O(n) time and with only 1 pass
- It uses O(n) extra memory
- Solution2:
- Sort the array using merge sort (O(nlogn) time)
- Parse again and if you see a element twice you got the dup.
- Pros:
- it doesn't use extra memory
- Running time is greater than O(n)
No comments:
Post a Comment