Amazon Interview Question: Given an array of integers, ... | Glassdoor

Interview Question

Quality Assurance Engineer Interview Seattle, WA

Given an array of integers, how would you move the

  duplicate integers into a different array? What is the efficiency of your algorithm?
Answer

Interview Answer

3 Answers

10

In Java I'd do this using a hashset and newly created array(of dups). Simply iterate over the array checking first to see if the value already existed in the hashset. If it already exists add to array(of dups) if not add to hashset.

Action on Feb 25, 2012
0

int[] arr = { 1, 3, 2, 4, 1, 3, 1};
        int[] arrDuplicate = new int[arr.length];
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = i+1; j < arr.length; j++) {
                if (arr[i] == arr[j]) {
                    arrDuplicate[count] = arr[j];
                    count++;
                }
            }
        }
        for (int k = 0; k< count ; k++) {
            System.out.println(arrDuplicate[k]);
        }

Nitesh on Apr 16, 2017
0

for (String name : names) {
     if (set.add(name) == false) {
        // your duplicate element
     }
}

Radha on Jul 14, 2017

Add Answers or Comments

To comment on this, Sign In or Sign Up.