• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help me for this assignment please  RSS feed

 
mohamed mousa
Greenhorn
Posts: 4
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.
So what have you tried so far and where are you stuck?
 
mohamed mousa
Greenhorn
Posts: 4
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

but the output is 1,2,3,4
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred is right; XOR is probably not the right way to do it.
 
mohamed mousa
Greenhorn
Posts: 4
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!