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                      
tip   To Refer this Site to Your Friends   Click Here
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 ?    1 Yes 1 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 ?    1 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 2 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 ?    0 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 ?    0 Yes 0 No
Ram
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
Give a oneline C expression to test whether a number is a power of 2? Motorola9
Write a program that find and print how many odd numbers in a binary tree  1
1) int i=5; j=i++ + i++ + i++; printf("%d",j);This code gives the answer 15.But if we replace the value of the j then anser is different?why? 2)int i=5; printf("%d",i++ + i++ + i++); this givs 18. Infosys3
How to swap two variables, without using third variable ? HCL28
Write a routine that prints out a 2-D array in spiral order Microsoft2
What is the main difference between STRUCTURE and UNION?  3
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. Microsoft3
Print an integer using only putchar. Try doing it without using extra storage.  1
write a program to Insert in a sorted list Microsoft4
How to read a directory in a C program?  3
Sorting entire link list using selection sort and insertion sort and calculating their time complexity NetApp1
How do you sort a Linked List (singly connected) in O(n) please mail to pawan.10k@gmail.com if u can find an anser...i m desperate to knw... Oracle3
plz send me all data structure related programs  1
Finding a number multiplication of 8 with out using arithmetic operator NetApp7
how can u draw a rectangle in C Wipro24
Given n nodes. Find the number of different structural binary trees that can be formed using the nodes. Aricent3
Give a very good method to count the number of ones in a 32 bit number. (caution: looping through testing each bit is not a solution) Microsoft4
String copy logic in one line. NetApp7
Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned long in decimal.  5
Program to find the largest sum of contiguous integers in the array. O(n)  5
 
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