Amazon Interview Question

given an array of numbers to remove the duplicates

Interview Answers

Anonymous

Aug 24, 2012

Sort the array in place. Then iterate and you can easily find duplicates by comparing the current and previous values. Sort is n log n; iterate is n. Total is n log n.

2

Anonymous

Oct 19, 2012

http://stackoverflow.com/questions/1532819/algorithm-efficient-way-to-remove-duplicate-integers-from-an-array

1

Anonymous

Aug 25, 2012

The simplest way is to use a set (no duplicate keys, and, if values are used as keys, no duplicate values). This way you simply need to iterate over the entire array and add each key/value into the set (it ignores adding it if it's a duplicate).

3

Anonymous

Aug 25, 2012

Matt, Can't use more memory.

Anonymous

Sep 6, 2012

1) Sort this array by using quicksort, so the time will be O(nlogn), 2) Start p1 pointer at beginning, and p2 at last and start move p1++ 3) if found p1 == previous p1, sway p2 and previous p1, and move p2--, the idea is trying to move all duplication to the end of arrary, 4) keep doing 3) till p1 = p2, 5) so we have a clear array without dup, then use quicksort again from index 0 - p1

1

Anonymous

Oct 2, 2012

if you want to remove duplicate element in the array, try this algo: num = 0; for (i =0; i < arr.length()-1; i++) { if (arr[i] < arr[i+1]) { arr[num++] = arr[1]; } } arr[num] = arr[n-1];

Anonymous

Apr 4, 2016

How about using HashTable or Dictionary to store the array values as keys. We can use ContainsKey method to skip over duplicates. Once the array elements are copied into the dictionary, we can convert the dictionary keys into an array to get a non-duplicated version of the original array This is constant space, but linear time. However this method will not keep the original order of the elements.

1

Anonymous

Jul 13, 2012

I'm not sure. Maybe use merge sort and throw away duplicate values.

2

Anonymous

May 3, 2015

Convert array to a set. If thats not an option, consider, counting sort if range known else merge sort as mentioned by some one earlier

Anonymous

Aug 24, 2012

That is the answer that I gave. The interviewer than asked "how do you delete the duplicates from the array." I couldn't figure out that part.

Anonymous

Mar 21, 2015

in C# 3.5+ , you can use LINQ query //given array arr arr.Distinct().ToArray(); removes duplicates