Question: https://codility.com/programmers/task/odd_occurrences_in_array

Question Name: Odd-Occurrences-In-Array or OddOccurrencesInArray

This question is completely the same as the Perm-Missing-Elem by codility. The comment from Kim is very helpful.

1 2 3 4 5 6 | def solution(A): result = 0 for number in A: result ^= number return result |

My Solution Using C#

https://codility.com/demo/results/training6NADS6-7EW/

Please could you explain what the ^= operator does in the expression?

Thanks.

Please refer to the post and comments in Perm-Missing-Elem by codility. Thanks!

dosn’t work with [7, 9, 3, 9, 3, 9, 3]

oh, sorry i’m dosn’t look at condition : “N is an odd integer within the range [1..1,000,000];”

That’s fine. Good to see that you catch the bug.

JavaScript solution

Why I am getting only 11% for correctness?

When you are using “for (var number in A)”, you are actually getting the indexes, such as [0, 1, 2, …], rather than the array elements.

You should use:

for (var number of A)

My Java solution (100%)

bro can u explain me one statement in else part “” i = j – 1; “”

Right after “i = j – 1”, it goes into for loop and run “i++”. Eventually, for the next iteration, i = j. And we are accessing the first next character that is not A[old i].

O(N^2) complexity

O(N^2) complexity

I’m quite new to programming but here’s my python solution. I didn’t realise I could have used XOR.

Welcome to the coding world 🙂

Your solution is working. That’s great! And the major improvement space is, it’s abusing the exception mechanism.

HI Sir

Could you please elucidate result ^= number this line of code. I have checked out the PermMissing problem and understood the XOR operation . However, I do not know what ^= will return

Thanks

Please check Kim’s comment. It’s very helpful.

ruby solution

all(map(lambda e: e%2, a)) # python built-in style

errr perhaps I should have read the *actual* question prior to answering.

Where can you get info on this operator, I can´t find it anywhere. How does it work?

Ruby’s built-in operator. I do not have any experience in Ruby. But it looks so.

Mi Javascript Solution 100%

My very simple Java solution (100%).

This is my solution in python

I got a 100% score

Looks correct and have O(N) complexity.

It does comply with time complexity, but I think it exceeds space compexlity of O(1)

Does anyone know what the ^= means? The Perm-Missing-Elem by codility post that keeps being references does not explain this.

It’s XOR.

In PHP 100% To All 😀

I see lots of solutions that call sort but the standard sorts are comparison sorts and have run time O(N logN). The question asks for O(N). That they pass the test indicates that the system is unable to distinguish O(N) and O(N log N) but I don’t believe they are correct per the instructions.

You are totally correct. The solutions with

`sort`

is not right, because of the time complexity.A ruby solution…

I have the following solution:

but have only Correctness 80% because of the error during check:

And this errors occurs everytime…

Can somebody explain what is the issue in my solution?

Thanks in advance!

Consider the example [1, 1, 1, 2, 2].

Another in Python, using set:

Hallo. This is my C++ version. I am not sure, I get 100% but I am wondering if it is 100% bug-proof. I modify the python version of @Ty.

And yes this is O(N logN) . I am wondering if is it possible to obtain something similar to the one of @Ivan Padron .

>> catch (exception& e) // I am not sure of this part

It’s used to catch the odd occurrence, when the largest number occurs for odd number of times.