ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Do you have a collection of Interview Questions and interested to share with us!!
Please send that collection to along with your userid / name. ThanQ
Google
 
Categories  >>  Code Snippets  >>  Programming Code  >>  C Code
 
 


 

 
 C Code interview questions  C Code Interview Questions
 C++ Code interview questions  C++ Code Interview Questions
 VC++ Code interview questions  VC++ Code Interview Questions
 Java Code interview questions  Java Code Interview Questions
 Dot Net Code interview questions  Dot Net Code Interview Questions
 Visual Basic Code interview questions  Visual Basic Code Interview Questions
 Programming Code AllOther interview questions  Programming Code AllOther Interview Questions
Question
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).
 Question Submitted By :: =-pkg-=
I also faced this Question!!     Rank Answer Posted By  
 
  Re: 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).
Answer
# 1
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 ?    4 Yes 4 No
Aparna Vutukuru
 
  Re: 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).
Answer
# 2
#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 ?    2 Yes 0 No
Raghuram.A
 
 
 
  Re: 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).
Answer
# 3
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 3 No
Sam
 
  Re: 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).
Answer
# 4
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 0 No
Neo
 
  Re: 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).
Answer
# 5
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 0 No
Neo
 
  Re: 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).
Answer
# 6
/* 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 0 No
Abraham
 
  Re: 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).
Answer
# 7
#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 ?    0 Yes 0 No
Suraj Bhan Gupta
 
  Re: 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).
Answer
# 8
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 ?    2 Yes 4 No
Ram
 
  Re: 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).
Answer
# 9
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 ?    0 Yes 0 No
John
 
  Re: 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).
Answer
# 10
/*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 0 No
Karl Miro
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
# include<stdio.h> aaa() { printf("hi"); } bbb(){ printf("hello"); } ccc(){ printf("bye"); } main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc; ptr[2](); }  1
main() { int i=5,j=6,z; printf("%d",i+++j); }  1
Is the following code legal? struct a { int x; struct a *b; }  1
main() { int i; float *pf; pf = (float *)&i; *pf = 100.00; printf("\n %d", i); } a. Runtime error. b. 100 c. Some Integer not 100 d. None of the above HCL1
main() { int *j; { int i=10; j=&i; } printf("%d",*j); }  1
main() { char *p = “ayqm”; printf(“%c”,++*(p++)); } TCS2
What is "far" and "near" pointers in "c"...?  3
main() { int i=400,j=300; printf("%d..%d"); }  1
How we print the table of 2 using for loop in c programing?  1
void main() { int i=10, j=2; int *ip= &i, *jp = &j; int k = *ip/*jp; printf(“%d”,k); }  1
main() { int i=0; for(;i++;printf("%d",i)) ; printf("%d",i); }  1
write a program in c to merge two array  1
int i; main(){ int t; for ( t=4;scanf("%d",&i)-t;printf("%d\n",i)) printf("%d--",t--); } // If the inputs are 0,1,2,3 find the o/p  1
main() { char string[]="Hello World"; display(string); } void display(char *string) { printf("%s",string); }  1
func(a,b) int a,b; { return( a= (a==b) ); } main() { int process(),func(); printf("The value of process is %d !\n ",process(func,3,6)); } process(pf,val1,val2) int (*pf) (); int val1,val2; { return((*pf) (val1,val2)); }  1
main() { printf("%d", out); } int out=100;  1
How we will connect multiple client ? (without using fork,thread) TelDNA2
main() { int k=1; printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE"); }  1
main(int argc, char **argv) { printf("enter the character"); getchar(); sum(argv[1],argv[2]); } sum(num1,num2) int num1,num2; { return num1+num2; }  1
void main() { int const * p=5; printf("%d",++(*p)); }  1
 
For more C Code Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com