About Us

Glassdoor is your free inside look at Google interview questions and advice. All interview reviews posted anonymously by Google employees and interview candidates.

Search

for

in

Interview Question for Engineering Staff at Google:

Oct 23, 2009

How do you remove duplicates in a large array.

Add Tags [?]
Helpful Question?  
Yes | No
Share: Link Digg

Answers & Comments (2)

Nov 10, 2009

by ellemeno:

Create a hash table with size equal to n, the number of items in the array. Set a counter, uniqueCount to 0. Then walk through the array. Check if a[i] is in the hash table. If it is not, add a[i] to the hash table with the unique count as its value. Increment the unique count. At the end, put the hash results into your array, using each key’s value as its index, and set all remaining elements to null. The worst-case for this solution is O(n), but it also requires O(n) memory to store the hash table. Is there a way to do this in place?

removeDupes(int[] a){
    int uniqueCount = 0;
    for (int i = 0; i < n; i++){
        if (a[i] not in hash table) {
            add a[i] to hash table with value uniqueCount;
            uniqueCount++;
        }
    }
    set = get key-value pairs from hash table;
    for (each element e in set) {
        a[e.value] = e;
    }
    for (int i = uniqueCount; i < n; i++){
        a[i] = null;
    }
}
Helpful Answer?  
Yes | No
Inappropriate?

Nov 11, 2009

by ellemeno:

If we are allowed to modify the array, we could do an in-place sort, O(n log n), and then a walk-through, deleting the duplicates. This solution would run in O(n log n) time and require constant memory. If the array’s elements can be mapped to unique integers and the largest of these integers is guaranteed to be less than 2^31, then we can use a bit array to store presence, which may require less memory than a hash table.
Helpful Answer?  
Yes | No
Inappropriate?
Post your anonymous review or sign in to answer or comment on this question
Google Overview (GOOG )
Web
www.google.com
Industries
Size
5000+ Employees, $21B+ Revenue
HQ
Mountain View, CA
Competitors


Tags are like keywords that help categorize interview questions that have something in common.

Flag this {0} as inappropriate

Would you like us to review something? Please describe the problem with this {0} and we will look into it.

Flag this {1} Cancel

Advanced Search Reset

What

Where

How

or Cancel