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 graph in c?
Why header files are used?
How many identifiers are there in c?
Why is c called a mid-level programming language?
Write a program to print numbers from 1 to 100 without using loop in c?
Can you mix old-style and new-style function syntax?
Explain what does the characters 'r' and 'w' mean when writing programs that will make use of files?
How important is structure in life?
What is linear search?
Can a local variable be volatile in c?
What are types of preprocessor in c?
Why pointers are used in c?
What does. int *x[](); means ?
What is the explanation for cyclic nature of data types in c?
What is 'bus error'?