## Expedia Interview Question

Principal Software Engineers**Country:**India

**Interview Type:**Phone Interview

The HashMap/HashSet are only used to detect duplicates. The order is kept by iterating over the array and storing the duplicates in another array or simply printing them out as we go.

I have solve it using count sort.I will create one more array of size K where K is the range of numbers it can have.Now suppose N numbers are there than the time complexity is

O(K * N)

let say numbers are 3,5,5,8,4,7,8,2,7,9,6,6

then another array will be of size 9 as numbers varies from 1-9

now in this array i will put the count of numbers at index = number , so

index 1 2 3 4 5 6 7 8 9

count 0 1 1 1 2 2 2 2 1

I will finally traverse the new array and any index with count > 1 means it is duplicate.

Time complexity of this algorithm is O(N), I do not think K will play any role, whereas it is possible for shorter arrays space complexity will be quite huge... Consider following array

1, 123456

Also, what if your array comprises negative integers? It seems you might need an additional step...

Create one hash map and start putting array elements one by one. Before putting elements in hash map just check whether it's already present in hash map or not. If already present in hash map then it's duplicate and print it.

Time Complexity = O(n)

Space Complexity = O(n)

This answer is incorrect as hashmap does not preserve the insertion order, for that we can used LinkedHashMap but then we can use it if space is not a constraint.

@Avinash :

1) HashMap is a map based on hashing of the keys. It supports O(1) get/put operations. Keys must have consistent implementations of hashCode() and equals() for this to work.

2) LinkedHashMap is very similar to HashMap, but it adds awareness to the order at which items are added (or accessed), so the iteration order is the same as insertion order (or access order, depending on construction parameters).

There is no guarantee that hash map will preserve the order in which it is inserted.Hence although we will find out the duplicates using hash map but the order will get change.

#include<stdio.h>

int main(){

int arr[50];

int *p;

int i,j,k,size,n;

printf("\nEnter size of the array: ");

scanf("%d",&n);

printf("\nEnter %d elements into the array: ",n);

for(i=0;i<n;i++)

scanf("%d",&arr[i]);

size=n;

p=arr;

for(i=0;i<size;i++){

for(j=0;j<size;j++){

if(i==j){

continue;

}

else if(*(p+i)==*(p+j)){

k=j;

size--;

while(k < size){

*(p+k)=*(p+k+1);

k++;

}

j=0;

}

}

}

printf("\nThe array after removing duplicates is: ");

for(i=0;i < size;i++){

printf(" %d",arr[i]);

}

return 0;

}

```
// Time Complexity is O(n) and space complexity is (n)
void findDuplicates(int[] arr) {
Map<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
Set<Integer> duplicates = new HashSet<Integer>();
for (int index = 0; index < arr.length; index++) {
if (null != hashMap.get(arr[index])) {
duplicates.add(arr[index]);
} else {
hashMap.put(arr[index], 1);
}
}
for (Integer duplicate : duplicates) {
System.out.println(duplicate);
}
}
```

public static List<int> FindDuplicates(int[] arr)

{

List<int> duplicates = new List<int>();

Dictionary<int, int> iDict = new Dictionary<int, int>();

foreach(int i in arr)

{

if(!iDict.ContainsKey(i))

{

iDict.Add(i, 1);

}

else

{

iDict[i]++;

}

}

foreach(KeyValuePair<int,int> keyvalue in iDict)

{

if(keyvalue.Value>1)

{

duplicates.Add(keyvalue.Key);

}

}

return duplicates;

}

There are 4 Approaches for this solution:

Approach 1: If input element is lying certain number range and upper limit of range is not too high.

Than apply counting sort approach

Approach 2: Using hash map, input array number is key for the element,hence when we insert element in hashmap check key exist or not, if now than add key with frequency 1 as value, otherwise increase frequency by 1.

Appraoch 3: Build BST from given number at any node , node having four filed, value, frequncy , left and right reference.

Use a set instead. For hashmap, it will need <key,value> pairs. Instead set will serve the purpose. Use contains method to find if the value is already inserted in the set. If yes, print the element.

- Anonymous December 26, 2013