Microsoft Interview Question
Use maps and insert each element into map. All duplicates will be removed automatically. The aray is not even required to be sorted.
Hi,
take two pts current and insert both pointinfg to 2nd element.
if the element pointed by current is not equal to
the element pointed by (insert-1) copy current element
at index insert and increment current and insert
by 1.
otherwise (if both are equal) increment current by 1.
int remove_duplicates(int * p, int size)
{
int current, insert = 1;
for (current=1; current < size; current++)
if (p[current] != p[insert-1])
{
p[insert] = p[current];
current++;
insert++;
} else
current++;
return (insert-1); //return the last index of
//array with unique elements
}
Here is my code....
#include<stdio.h>
#include<stdlib.h>
void main()
{
int a[100],n=0,i, j;
printf("Enter the total no. of elements in the array\n");
scanf("%d", &n);
printf("Enter the elements in sorted order \n");
for(i=0;i<n;i++)
scanf("%d", &a[i]);
j = 0;
for(i=0;i<n-1;i++)
{
if(a[i] > a[i+1])
{
printf("Array not sorted \n");
exit(0);
}
while(a[i] == a[i+1])
i++;
a[j++] = a[i];
}
if(i!=n)
a[j++] = a[n-1];
printf("The new array is \n");
for(i = 0; i<j; i++)
printf("%d ", a[i]);
}
int array[15] = { 1,1,1,2,2,3,3,4,4,4,5,5,6,7,7 }
I assumed that array size is given to us. let suppose it is N
1. start traversing the array from end.
2. compare current and next element if they are equal
then shift the array to next element location.
( Remember shifting of an element doesn't considered in complexity )
3. else cont. the loop
so time complexity O(N)
//copy the unique items to a new array and then copy it back
public static void remove_newarray(int [] array){
int[] a=new int[array.length];
a[0]=array[0];
int count=0,j=0;
for(int i=1;i<array.length&&count<(array.length-1);i++){
while(count<(array.length-1)&&a[count]==array[i]){
i++;
}
count++;
a[count]=array[i];
}
for(j=0;j<count+1;j++){
array[j]=a[j];
}
while(j<array.length){
array[j]=0;//use 0 to fill in the extra spaces in the array
j++;
}
}
//another approach is to use hashtable and then copy back to the array, if the sorted order is not required
public static void remove_hash(int [] array){
Hashtable<Integer, Integer> ht=new Hashtable<Integer, Integer>();
for(int i=0;i<array.length;i++){
ht.put(array[i], i);
array[i]=0;//clear the original array
}
int count=0;
for(int t:ht.keySet()){
array[count]=t;
count++;
}
}
//use two pointers in the original array
public static void remove_pointer(int[] array){
int p1=0,p2=p1+1;
while(p1<array.length&&p2<array.length){
while(p1<array.length&&p2<array.length&&array[p2]==array[p1]){
p2++;
}
p1++;
if(p2<array.length){
array[p1]=array[p2];
}
else
break;
}
System.out.println(p1);
while(p1<p2){
array[p1]=0;
p1++;
}
}
public class DuplicatesRemover {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int array[] = {1,2,3,3,4,5,5,6,7,7,8,9,9};
int f = 0;
int s = 1;
while(s != array.length){
if(array[f] != array[s]){
array[f+1] = array[s];
f = f+1;
s = s+1;
}
else{
s = s+1;
}
}
f++;
while(f < array.length){
array[f++] = 0;
}
for(int i : array){
System.out.print(i+" ");
}
}
}
1st option is to create a new array with one occurance of each number.
- Eila June 13, 20062nd option is to hold 2 pointers:
and compare the first pounter value with the 2nd pointer value.
if the values are equal increase the 2nd pointer and compare the next value with teh first pointer.
if the value are different and the 1st and the second pointers are more than 1 entry difference, put the second pointer value in the 1st pointer address + 1. and increase the first position address value
Eventually, you will have the original array with one occurance of each value. and the array last value will be first pointer place