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
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 |
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 |
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 |
program to find magic aquare using array
main() { int i=4,j=7; j = j || i++ && printf("YOU CAN"); printf("%d %d", i, j); }
Can you send Code for Run Length Encoding Of BMP Image in C Language in linux(i.e Compression and Decompression) ?
#define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); }
main() { int i=0; while(+(+i--)!=0) i-=i++; printf("%d",i); }
9 Answers CSC, GoDB Tech, IBM,
Write a program to implement the motion of a bouncing ball using a downward gravitational force and a ground-plane friction force. Initially the ball is to be projected in to space with a given velocity vector
void main() { char a[]="12345\0"; int i=strlen(a); printf("here in 3 %d\n",++i); }
main() { int i = 100; clrscr(); printf("%d", sizeof(sizeof(i))); } a. 2 b. 100 c. 4 d. none of the above
main() { unsigned int i=10; while(i-->=0) printf("%u ",i); }
typedef struct error{int warning, error, exception;}error; main() { error g1; g1.error =1; printf("%d",g1.error); }
how to test pierrot divisor
Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.