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

How will you print % character? a. printf(“\%”) b. printf(“\\%”) c. printf(“%%”) d. printf(“\%%”)

4 Answers   HCL,


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,


main() { char name[10],s[12]; scanf(" \"%[^\"]\"",s); } How scanf will execute?

2 Answers  


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() { char i=0; for(;i>=0;i++) ; printf("%d\n",i); }

2 Answers  






Write a C program to print ‘Campus Force training’ without using even a single semicolon in the program.

3 Answers   Wipro,


void main() { int i; char a[]="\0"; if(printf("%s\n",a)) printf("Ok here \n"); else printf("Forget it\n"); }

3 Answers   Accenture,


Code for 1>"ascii to string" 2>"string to ascii"

1 Answers   Aricent, Global Logic,


why nlogn is the lower limit of any sort algorithm?

0 Answers  


Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba. You can assume that all the characters will be unique.

5 Answers   IITR, Microsoft, Nike,


1 o 1 1 0 1 0 1 0 1 1 0 1 0 1 how to design this function format in c-language ?

2 Answers  


There were 10 records stored in “somefile.dat” but the following program printed 11 names. What went wrong? void main() { struct student { char name[30], rollno[6]; }stud; FILE *fp = fopen(“somefile.dat”,”r”); while(!feof(fp)) { fread(&stud, sizeof(stud), 1 , fp); puts(stud.name); } }

1 Answers  


Categories