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

int main() { int x=10; printf("x=%d, count of earlier print=%d", x,printf("x=%d, y=%d",x,--x)); getch(); } ================================================== returns error>> ld returned 1 exit status =================================================== Does it have something to do with printf() inside another printf().

1 Answers  


main() { char c; int i = 456; clrscr(); c = i; printf("%d", c); } a. 456 b. -456 c. random number d. none of the above

3 Answers   BrickRed, HCL,


what is the code of the output of print the 10 fibonacci number series

2 Answers  


main() { int x=5; clrscr(); for(;x==0;x--) { printf("x=%d\n”", x--); } } a. 4, 3, 2, 1, 0 b. 1, 2, 3, 4, 5 c. 0, 1, 2, 3, 4 d. none of the above

3 Answers   HCL,


what is brs test reply me email me kashifabbas514@gmail.com

0 Answers  






write a c-program to find gcd using recursive functions

5 Answers   HTC, Infotech,


const int perplexed = 2; #define perplexed 3 main() { #ifdef perplexed #undef perplexed #define perplexed 4 #endif printf("%d",perplexed); } a. 0 b. 2 c. 4 d. none of the above

1 Answers   emc2, HCL,


main() { char a[4]="HELL"; printf("%s",a); }

3 Answers   Wipro,


How will u find whether a linked list has a loop or not?

8 Answers   Microsoft,


main() { unsigned char i=0; for(;i>=0;i++) ; printf("%d\n",i); }

1 Answers  


typedef struct error{int warning, error, exception;}error; main() { error g1; g1.error =1; printf("%d",g1.error); }

1 Answers  


main() { int i; printf("%d",scanf("%d",&i)); // value 10 is given as input here }

2 Answers   IBM,


Categories