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 |
void main() { if(~0 == (unsigned int)-1) printf(“You can answer this if you know how values are represented in memory”); }
What is the difference between proc means and proc tabulate ? explain with a simple example when you have to use means or tabulate?
write a c program to Create employee record by taking details like name, employee id, address and phone number. While taking the phone number, take either landline or mobile number. Ensure that the phone numbers of the employee are unique. Also display all the details
main() { char string[]="Hello World"; display(string); } void display(char *string) { printf("%s",string); }
void pascal f(int i,int j,int k) { printf(“%d %d %d”,i, j, k); } void cdecl f(int i,int j,int k) { printf(“%d %d %d”,i, j, k); } main() { int i=10; f(i++,i++,i++); printf(" %d\n",i); i=10; f(i++,i++,i++); printf(" %d",i); }
write a c-program to display the time using FOR loop
Design an implement of the inputs functions for event mode
/*what is the output for*/ void main() { int r; printf("Naveen"); r=printf(); getch(); }
posted by surbhi just now main() { float a = 5.375; char *p; int i; p=(char*)&a; for(i=0;i<=3;i++) printf("%02x",(unsigned char) p[i]); } how is the output of this program is :: 0000ac40 please let me know y this output has come
Is the following code legal? typedef struct a aType; struct a { int x; aType *b; };
write a program to Insert in a sorted list
how to swap 3 nos without using temporary variable