## VMWare Inc Interview Question

Member Technical Staffs**Country:**India

**Interview Type:**In-Person

#include<stdio.h>

#include<conio.h>

int FindDuplicate(int a[],int n)

{

int temp=a[0];

for(int i=1;i<n;i++)

{

temp=temp^a[i];

}

return temp;

}

void main()

{

int i, n;

int a[20];

printf("Enter the Number of Elements u want to Enter");

scanf("%d",&n);

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

scanf("%d",a[i]);

i=FindDuplicate(a,n);

}

What is the space complexity ?

If we know the max number in the array, then we can create an alternate array and use the number as index and keep track of the number of times each number has come.

For example:

```
int main()
{
int arr[4] = { 1, 2, 1,3};
int brr[4] = { 0, 0, 0, 0};
for ( int i = 0 ; i < 4 ; ++i) {
brr[arr[i]]++;
}
for ( int i = 0 ; i <4 ; ++i) {
if ( brr[i] == 2) {
cout << "Duplicate with k entries: " << i << endl;
}
}
return 0;
}
```

Now, Here with value of k = 2, we get the duplicate number as 1.

if original array contains large numbers with sparse entries then size of new array will be very large, which will be very inefficient and wastage of memory.

e.g. arr = {-12, 100, 3, 9834}

in this case you need an array of size 9834. Moreover one entry is -ve also. so brr[arr[i]] will result in coredump!! or segmentation fault.

A better solution is to use hashing.

if original array contains large numbers with sparse entries then size of new array will be very large, which will be very inefficient and wastage of memory.

e.g. arr = {-12, 100, 3, 9834}

in this case you need an array of size 9834. Moreover one entry is -ve also. so brr[arr[i]] will result in coredump!! or segmentation fault.

A better solution is to use hashing.

if original array contains large numbers with sparse entries then size of new array will be very large, which will be very inefficient and wastage of memory.

e.g. arr = {-12, 100, 3, 9834}

in this case you need an array of size 9834. Moreover one entry is -ve also. so brr[arr[i]] will result in coredump!! or segmentation fault.

A better solution is to use hashing.

//#include<stdio.h>

#include<iostream>

#include <iterator>

#include <map>

using namespace std;

int main()

{

int arr[10]={2,10,11,9,2,4,3,9,12,2};

map<int,int> um;

int count;

map<int,int> :: iterator it;

for (int i = 0 ; i<=9 ; i++)

{

count = 0;

it = um.find(arr[i]);

if(it!=um.end())

{

(*it).second = ((*it).second) + 1;

}

else

um.insert(make_pair(arr[i],++count));

}

map<int,int> :: const_iterator it1;

for(it1 = um.begin();it1!= um.end();it1++)

{

cout<<(*it1).first <<" "<<(*it1).second<<endl;

}

return 0;

}

```
#include<iostream>
#include <iterator>
#include <map>
using namespace std;
int main()
{
int arr[10]={2,10,11,9,2,4,3,9,12,2};
map<int,int> um;
int count;
map<int,int> :: iterator it;
for (int i = 0 ; i<=9 ; i++)
{
count = 0;
it = um.find(arr[i]);
if(it!=um.end())
{
(*it).second = ((*it).second) + 1;
}
else
um.insert(make_pair(arr[i],++count));
}
map<int,int> :: const_iterator it1;
for(it1 = um.begin();it1!= um.end();it1++)
{
cout<<(*it1).first <<" "<<(*it1).second<<endl;
}
return 0;
}
```

Use dictionary where key will be an array item and value would be count and increase count every time you find duplicate. Time complexity would be O(n). I am assuming array is not sorted

- Amit August 02, 2015