Write, efficient code for extracting unique elements from
a sorted list of array.

e.g. (1, 1, 3, 3, 3, 5, 5, 5, 9, 9, 9, 9) -> (1, 3, 5, 9).

Answers were Sorted based on User's Feedback



Write, efficient code for extracting unique elements from a sorted list of array. e.g. (1,..

Answer / abraham

/* Using the same array and no extra storage space*/
/* "Nothing's far when one wants to get there." */

#include<stdio.h>

main()
{
int a[] = {1, 1, 3, 3, 3, 5, 5, 5, 9, 9, 9,10};
int cur = 0;
int i = 0;

for (i = 1 ; i < (sizeof(a)/4) ;i++)
{
if ( a[cur] != a[i])
{
++cur;
if (cur != i)
{
a[cur] = a[i];
}
}
}
a[++cur] = '\0';
for (i =0 ; i< cur;i++)
printf ("%d\n",a[i]);
}

Is This Answer Correct ?    2 Yes 6 No

Write, efficient code for extracting unique elements from a sorted list of array. e.g. (1,..

Answer / sam

private int[] GetUniqueList(int[] A)
{

if(A == null ) return null;
List<int> result = new List<int>();
for(int i=0;i<A.Length-1;i++)
{
if (A[i] != A[i + 1]) result.Add(A[i]);
}
if(A.Length>0)
result.Add(A[A.Length-1]);
return result.ToArray();
}

Is This Answer Correct ?    0 Yes 6 No

Write, efficient code for extracting unique elements from a sorted list of array. e.g. (1,..

Answer / aparna vutukuru

void main()
{
int a[15],i,b;
void unique(int[],int);
printf("enter the array elements");
for(i=0;i<15;i++)
scanf("%d",&a[i]);
clrscr();
b=a[0];
printf("%d",b);
unique(a,b);
getch();
}
void unique(int a[],int start)
{
int b,i;
b=start;
for(i=0;i<15;i++)
{
if(a[i]!=b)
{
b=a[i];
printf("%d",b);
}
}
}

Is This Answer Correct ?    12 Yes 20 No

Post New Answer

More C Code Interview Questions

Is it possible to print a name without using commas, double quotes,semi-colons?

7 Answers  


How do you sort a Linked List (singly connected) in O(n) please mail to pawan.10k@gmail.com if u can find an anser...i m desperate to knw...

6 Answers   Microsoft, MSD, Oracle,


void main() { int *mptr, *cptr; mptr = (int*)malloc(sizeof(int)); printf(“%d”,*mptr); int *cptr = (int*)calloc(sizeof(int),1); printf(“%d”,*cptr); }

1 Answers  


Derive expression for converting RGB color parameters to HSV values

1 Answers  


void func1(int (*a)[10]) { printf("Ok it works"); } void func2(int a[][10]) { printf("Will this work?"); } main() { int a[10][10]; func1(a); func2(a); } a. Ok it works b. Will this work? c. Ok it worksWill this work? d. None of the above

1 Answers   HCL,






#define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); }

3 Answers  


write the function. if all the character in string B appear in string A, return true, otherwise return false.

11 Answers   Google,


Given a list of numbers ( fixed list) Now given any other list, how can you efficiently find out if there is any element in the second list that is an element of the first list (fixed list)

3 Answers   Disney, Google, ZS Associates,


main() { int k=1; printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE"); }

1 Answers  


# include<stdio.h> aaa() { printf("hi"); } bbb(){ printf("hello"); } ccc(){ printf("bye"); } main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc; ptr[2](); }

1 Answers  


main() { int a=10,*j; void *k; j=k=&a; j++; k++; printf("\n %u %u ",j,k); }

1 Answers  


main() { int x=5; clrscr(); for(;x<= 0;x--) { printf("x=%d ", x--); } } a. 5, 3, 1 b. 5, 2, 1, c. 5, 3, 1, -1, 3 d. –3, -1, 1, 3, 5

2 Answers   HCL,


Categories