posted 4 years ago

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.

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.

Campbell Ritchie

Marshal

Posts: 56533

172

posted 4 years ago

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.

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Campbell Ritchie

Marshal

Posts: 56533

172

posted 4 years ago

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)

SCJP

Visit my download page

posted 4 years ago

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.

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.

To earn money on java go to upwork.com

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