Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


Given an array of numbers, except for one number all the
others occur twice. Give an algorithm to find that number
which occurs only once in the array.

Answers were Sorted based on User's Feedback



Given an array of numbers, except for one number all the others occur twice. Give an algorithm to ..

Answer / deepak

/*
you can also do some pre tests like array length should be odd.

just take an XOR of all the numbers it will give u the
number that occured single time.

it assumes that data is in correct form i.e. there is one
and only one number that occurs once.
*/
public static int whoOccursSingleTime(int a[]){
int s=0;
for(int i=0;i<a.length;i++){
s=s^a[i];
}
return s;
}

Is This Answer Correct ?    19 Yes 3 No

Given an array of numbers, except for one number all the others occur twice. Give an algorithm to ..

Answer / priyanshu

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
long int find(long int a[],long int n,long int f,long int l)
{
long int mid;
mid=(f+l)/2;
if(a[mid]==a[mid+1])
{
return find(a,mid,mid+1,n-1);
}
else if(a[mid]==a[mid-1])
{
return find(a,mid,0,mid-1);
}
else
return a[mid];
}

int main()
{
long int n,i;
scanf("%ld",&n);
long int a[n];
for(i=0;i<n;i++)
scanf("%ld",&a[i]);
sort(a,a+n);
printf("%ld",find(a,n,0,n-1));
return 0;
}

Is This Answer Correct ?    2 Yes 0 No

Given an array of numbers, except for one number all the others occur twice. Give an algorithm to ..

Answer / pushpa

int num[3];
int i,j,p=0,count_1=0,count=0,n;

n= 3;
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
}


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(num[i] == num[j])
count_1=0;
else
count++;
}
if(count == n-1)
{
printf("The number which occurs once is %d\n",num[i]);
}
count = 0;

}

Is This Answer Correct ?    1 Yes 1 No

Given an array of numbers, except for one number all the others occur twice. Give an algorithm to ..

Answer / don151

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,n,b[10],m,k,temp;
clrscr();
printf("eneett the v vallrue of n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("before sorting array");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[j]>=a[i])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
printf("after sorting");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
for(i=0;i<n;i+=2)
{
if(a[i]!=a[i+1])
{
printf("resutl for %d,%d\n",i,a[i]);
i++;
}
}
getch();
}

Is This Answer Correct ?    0 Yes 0 No

Given an array of numbers, except for one number all the others occur twice. Give an algorithm to ..

Answer / ruchi

#include<stdio.h>
#include<conio.h>
int main()
{
int a[15],i,j,n,temp,p,k;

printf("\nHow many elements are there in a array ");
scanf("%d",&n);
printf("\nEnter the elements ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nThe element which occur once is ");
for(i=0;i<n;i++)
{
k=2*i;
p=2*i+1;

if(a[k]!=a[p])
{
printf("%d\n",a[k]);
break;
}
}

getch();
}

Is This Answer Correct ?    0 Yes 3 No

Given an array of numbers, except for one number all the others occur twice. Give an algorithm to ..

Answer / guest

int arr[n]; //n' numbers

for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
{ if ( (a[i]==a[j])&&i!=j ) break; //ITS NOT THE NUM
if ( (a[i]!=a[j])&&j==n-1) return i; //IT IS!!
return -1; //IF THERE IS NO SUCH NUMBER...

Is This Answer Correct ?    3 Yes 7 No

Post New Answer

More C Interview Questions

What is indirect recursion? give an example?

4 Answers  


what is the different between if-else and switch statment (other than syntax)

26 Answers   CTS, Oracle, Scorpos,


hi any body pls give me company name interview conduct "c" language only

0 Answers  


How can I handle floating-point exceptions gracefully?

0 Answers  


count the numbers between 100 and 300, that star with 2 and ends with 2

5 Answers   Mind Tree,


do you think its fraud or original company?

0 Answers  


write a program to delete an item from a particular location of an linear array?

1 Answers  


What is true about the following C Functions a.Need not return any value b.Should always return an integer c.Should always return a float d.Should always return more than one value.

11 Answers   TCS,


How can I get random integers in a certain range?

0 Answers  


What is c language in simple words?

0 Answers  


Is anything faster than c?

0 Answers  


how to print "hai" in c?

13 Answers   TCS,


Categories