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
how to execute a program using if else condition and the output should enter number and the number is odd only...
cavium networks written test pattern ..
Describe the header file and its usage in c programming?
Why do we use main function?
How to throw some light on the b tree?
Explain what is the general form of a c program?
What are the loops in c?
writ a program to compare using strcmp VIVA and viva with its output.
What is FIFO?
Describe dynamic data structure in c programming language?
What is a null pointer in c?
What is difference between class and structure?
Which is better malloc or calloc?
If one class contains another class as a member, in what order are the two class constructors called a) Constructor for the member class is called first b) Constructor for the member class is called second c) Only one of the constructors is called d) all of the above
What are the 5 types of organizational structures?