programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Help me for this assignment please

mohamed mousa
Greenhorn
Posts: 4
Given an array of 2N+1 elements, N elements occur twice in the array. Find the element that occurs in the array only once.
You are expected to complete the function getUniqueElement(),
which takes the array as a parameter and return the unique element present in it.
Constraints:
0 ? N ? 100000 and each element would range between 0 to 10^9
Sample Test Cases:

Input #00:
1 4 2 2 3 3 1

Output #00:
4

Explanation:
In the given array only 4 doesn't occur twice.

Maneesh Godbole
Bartender
Posts: 11445
18
Welcome to the Ranch.
So what have you tried so far and where are you stuck?

mohamed mousa
Greenhorn
Posts: 4

but the output is 1,2,3,4

Campbell Ritchie
Marshal
Posts: 56533
172
Welcome again. Please write down on paper how you would do it by hand. Then show us what you have got. Once such correct instructions are written very simply, you can usually turn them into code easily.

mohamed mousa
Greenhorn
Posts: 4
i want solve this problem by XOR but i want how i solve it i dont know XOR

fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
mohamed mousa wrote:i want solve this problem by XOR but i want how i solve it i dont know XOR

if you don't know XOR, how do you know it is the right way to solve the problem? That's kind of like saying "I want to turn a screw with a table saw, but I don't know how to use a table saw".

Don't decide WHAT TO USE before you understand HOW TO DO IT. As Campbell said...figure out the steps on paper first. Pretend you had to tell a child how to do it using only paper and pencil. Then, revise those steps making them simpler and simpler.

you should probably spend 70-90% of your time THINKING about the problem, and only 10-30% CODING.

Campbell Ritchie
Marshal
Posts: 56533
172
Fred is right; XOR is probably not the right way to do it.

mohamed mousa
Greenhorn
Posts: 4
ok guys i will try it again and i will work by your advice
and i will post my code after i try for check it
thanks all

fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
There is no need to shout. I removed the formatting so that you don't scare people away.

Randall Twede
Ranch Hand
Posts: 4696
8
without thinking too hard about it(there could be problems), i would do it like this.. i would go through the array and put them in a second array. as i checked each one i would check to see if they are in the second array(repeats)

Bin Smith
Ranch Hand
Posts: 514
1
Hello!

I have been thinking on this task yesterday.
It seems to me that one way to solve this is to sort array at first and then you can divide its length by 2
In your case 7/2 gives 3. Next you do
if(array[2] !=array[3]){
//in this case you look up single number on the left of array that is from array[0] to array[2] including
} else {
//in this case you look up single element on the right from array[3] to array[6] including
}
So you extract left or right part of array and divide it by 2 again and do that comparison.

 Don't get me started about those stupid light bulbs.