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 / sandeep tiwari

void main()
{
int i,j;
int a[11]={1,1,3,3,3,5,5,5,9,9,9,9};
clrscr();
i=a[0];
printf("%d",i);

for(j=1;j<11;j++)
{
if(a[j]!=a[i])
{
if(a[j-1]==a[j])
continue;
else
printf("%d",a[j]);
i=j;
}
}
getch();
}
}

Is This Answer Correct ?    9 Yes 4 No

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

Answer / raghuram.a

#include<stdio.h>
#include<conio.h>
int getarray(int p[100], int n)
{
int c, i = 1;
for (c=1; c <n; )
if (p[c] != p[i-1])
{
p[i] = p[c];
c++;
i++;
} else
c++;
return i;
}
main()
{
int a[100],n,i,j;
printf("enter n:");
scanf("%d",&n);
printf("enter elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
j=getarray(a,n);
printf("new array is:");
for(i=0;i<j;i++)
printf("\t%d",a[i]);
getch();
return 0;
}

Is This Answer Correct ?    12 Yes 9 No

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

Answer / msvinod

/*This c program aimed at printing the unique numbers in a
sorted array of repeating numbers.*/
#include<stdio.h>
main(){
int len=0,i=0,rep=0; /*Initializing the identifiers*/
printf("Enter length of array :--");
scanf("%d",&len); /*Input for array length*/
int arr[len]; /*Initializing array*/
for(i=0;i<len;i++){ /*Running loop for input of array
elements*/
printf("%dth element :--",i);
scanf("%d",&arr[i]);
}
rep=arr[0]; /*Assigning array first value to rep*/
printf("Unique array :-- [%d,",rep); /*Printing of array
starts here*/
for(i=0;i<len;i++){ /*Running loop to print all the
unique elements of array*/
if(arr[i]!=rep){
rep=arr[i]; /*Assaigning new value to rep*/
printf("%d,",rep);
}
}
printf("]\n"); /*Closing array output*/
}
/*THE END*/

Is This Answer Correct ?    3 Yes 0 No

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

Answer / suraj bhan gupta

#include<stdio.h>
main(){
int a[10],start,i,c=1;
clrscr();
printf(" Enter ten elements in sorted order : ");
for (i=0 ; i<10 ; i++){
scanf("%d",&a[i]);
}

start = a[0];
for (i=1 ; i<10 ; i++){
if (start == a[i]){
c++; // we can eliminate
variable c using only index of array
}else{
printf(" %d no %d times
present",start,c);
start = a[i];
c=1;
}
}
getch();
}

Is This Answer Correct ?    3 Yes 1 No

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

Answer / ram

main ()
{

int a[] = { 1, 1, 3, 3, 3, 5, 5, 5, 9, 9, 9, 9 };
int i = 0, key;
int size;

size = sizeof (a) / sizeof (int);
key = a[i];

for (; i < size; i++)
{
if (key != a[i])
{
printf ("\n %d", a[i]);
key=a[i];
}
}
}

Is This Answer Correct ?    7 Yes 6 No

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

Answer / john

int a[] = { 1, 3, 1, 5, 1, 9, 4, 1, 3, 2, 3, 9 };
int* b;
int i = 0;
int max = 0;
int size = sizeof(a)/sizeof(int);

//find biggest element in array
for(; i < size; i++){
if(a[i] > max)
max = a[i];
}

//fill b with 0's
b = malloc(max*sizeof(int));
for(i = 0; i < max; i++){
b[i] = 0;
}

//use b to check for duplicates
for(i = 0; i < 12; i++){
b[a[i]]++;
if(b[a[i]] == 1)
printf("%d\n", a[i]);
}

Is This Answer Correct ?    1 Yes 1 No

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

Answer / karl miro

/*this program only stores a maximum of 30 unique possitive elements*/
#include<stdio.h>
#include<conio.h>
#define SIZE 30 /*change the value of size to adjust
maximum size of array*/
void main()
{
int array[SIZE],cnt=0,temp,i,flag;
clrscr();
do
{
flag=1;
printf("Please input an element(input -1 to end):");
scanf("%d",&temp);
if(temp==-1)
{break;}
else
for(i=0;i<cnt;i++)
{
if(temp==array[i])
flag=0;
}
if(flag)
{
array[i]=temp;
cnt++;
}
}while(temp!=-1);
printf("The elements are:\n");
for(i=0;i<cnt;i++)
printf(" %d ",array[i]);
}

Is This Answer Correct ?    1 Yes 1 No

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

Answer / karthikeyan s

/* Using LinkedHashSet concept */
/* No duplicate checking */
import java.util.*;

class Main
{
public static void main(String args[])
{
Scanner obj=new Scanner(System.in);
LinkedHashSet <Integer> l1=new LinkedHashSet<Integer>();
int n=obj.nextInt();
int a=0;
while(n-->0)
{
a=obj.nextInt();
l1.add(a);
}
System.out.println(l1 + " ");
}
}

Is This Answer Correct ?    0 Yes 0 No

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

Answer / neo

didn't find the posts good enough, was looking for a very
structured code, professional:

<CODE>
{
int i, j, iLen;
char *sTmpStr;

iLen = (int) strlen(sStr);
sTmpStr = (char *)malloc(sizeof(char) * iLen);

for(i = 0;i < iLen; i++)
sTmpStr[i] = sStr[i];

for(i = 0, j = iLen - 1; i < iLen/2; i++, j--)
sStr[i] = sTmpStr[j];
free(sTmpStr);
return;
}


int main()
{
char sStr1[] = {"This is an awful string"};
char sStr2[] = {"This is a K...O...O...L string"};

printf("String 1: %s\n", sStr1);
StrRevNoTempVar(sStr1);
printf("String 1 Rev: %s\n", sStr1);

printf("String 2: %s\n", sStr2);
StrRevNoTempVar(sStr2);
printf("String 2 Rev: %s\n", sStr2);

return 0;
}
</CODE>

Is This Answer Correct ?    0 Yes 4 No

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

Answer / neo

Crap <Posted Wrong Piece>, excuse.

<CODE>
// 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).

#include <stdio.h>


void RemoveDumInSortedList(int *iArr, int iSize)
{
int i, j, k, iCurrNo = 0;

for(i = 0, j = i + 1; j < iSize;)
{

if(iArr[j] == iArr[i])
{
j++;
}
else
{
iArr[++i] = iArr[j];
j++;
}
}

for(j = 0; j <= i; j++)
printf("ARRAY[%d] = %d\n", j, iArr[j]);
}


int main()
{
int iArr1[] = {1, 1, 3, 3, 3, 5, 5, 5, 9};
int iArr2[] = {1, 2, 2, 3, 4, 5, 5};

RemoveDumInSortedList(iArr2, sizeof(iArr2)/sizeof
(int));

return 0;
}
</CODE>

Is This Answer Correct ?    0 Yes 4 No

Post New Answer

More C Code Interview Questions

A program that will create a movie seat reservation. The program will display the summary seats and its status. The user will be ask what seat no. to be reserved, then it will go back again to the summary to display the updated seat status. If the seat no. is already reserved then it will prompt an error message. And also if the input seat no is out of range then it will also prompt an error message. The program is continuously running. Termination of the program will depends on how the programmer will apply. Sample output: Movie Seats Reservation Summary of Seats: Seat 1: Available Seat 2: Available Seat 3: Available Seat 4: Available Seat 5: Available Enter seat no. (Press 0 to terminate Or the assigned seat capacity) : 1 Movie Seats Reservation Summary of Seats: Seat 1: Reserve Seat 2: Available Seat 3: Available Seat 4: Available Seat 5: Available Enter seat no. (Press 0 to terminate Or the assigned seat capacity) : 6 The Seat no. is out of range! Movie Seats Reservation Summary of Seats: Seat 1: Reserve Seat 2: Available Seat 3: Available Seat 4: Available Seat 5: Available Enter seat no. (Press 0 to terminate Or the assigned seat capacity) : 1 The Seat no. is already reserved! Movie Seats Reservation Summary of Seats: Seat 1: Reserve Seat 2: Available Seat 3: Available Seat 4: Available Seat 5: Available Enter seat no. (Press 0 to terminate Or the assigned seat capacity) : 0 GoodBye... Thank You!!!

0 Answers  


hello sir,is there any function in C that can calculate number of digits in an int type variable,suppose:int a=123; 3 digits in a.what ll b answer?

6 Answers  


main() { extern int i; i=20; printf("%d",sizeof(i)); }

2 Answers  


given integer number,write a program that displays the number as follows: First line :all digits second line : all except the first digit . . . . Last line : the last digit

8 Answers  


What is the difference between proc means and proc tabulate ? explain with a simple example when you have to use means or tabulate?

1 Answers  






main() { int i=10; i=!i>14; Printf ("i=%d",i); }

1 Answers  


Write a function to find the depth of a binary tree.

13 Answers   Adobe, Amazon, EFI, Imagination Technologies,


void main() { void *v; int integer=2; int *i=&integer; v=i; printf("%d",(int*)*v); }

1 Answers   Honeywell,


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() { signed int bit=512, i=5; for(;i;i--) { printf("%d\n", bit >> (i - (i -1))); } } a. 512, 256, 0, 0, 0 b. 256, 256, 0, 0, 0 c. 512, 512, 512, 512, 512 d. 256, 256, 256, 256, 256

2 Answers   HCL,


String reverse with time complexity of n/2 with out using temporary variable.

10 Answers   NetApp, Symantec,


why do you use macros? Explain a situation where you had to incorporate macros in your proc report? use a simple instream data example with code ?

0 Answers  


Categories