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.

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is graph in c?

1054


Why header files are used?

1118


How many identifiers are there in c?

1018


Why is c called a mid-level programming language?

1198


Write a program to print numbers from 1 to 100 without using loop in c?

1079


Can you mix old-style and new-style function syntax?

1098


Explain what does the characters 'r' and 'w' mean when writing programs that will make use of files?

1250


How important is structure in life?

1082


What is linear search?

1132


Can a local variable be volatile in c?

987


What are types of preprocessor in c?

1066


Why pointers are used in c?

1006


What does. int *x[](); means ?

1064


What is the explanation for cyclic nature of data types in c?

1207


What is 'bus error'?

1144