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
What is clrscr in c?
What is the difference between a function and a method in c?
How is pointer initialized in c?
What is mean by Data Driven framework in QTP? Can any one answer me in details on this regard.
What do header files do?
What is difference between far and near pointers?
Describe the modifier in c?
What is the explanation for the dangling pointer in c?
How does free() know explain how much memory to release?
What is the 'named constructor idiom'?
What is a null pointer in c?
What are the types of pointers in c?
What is main return c?
Explain continue keyword in c
Are the variables argc and argv are always local to main?