I am trying to solve the following codility problem
in JS, and I want to achieve a decent Big-O notation score.
https://app.codility.com/programmers/lessons/2-arrays/odd_occurrences_in_array/
I'm completely stuck. I cannot figure out a way to do it without writing two loops. This gets me O(N**2)
for the complexity, which is confusing as at most it is O(N + N)
, right?
function solution(A) {
if(A.length === 1 || !A){
return A[0];
}
const counts = {};
for(let i in A){
if(!counts[A[i]]){
counts[A[i]] = 1
} else {
counts[A[i]] = 2
}
}
return +getNotDivisibleByTwo(counts);
}
function getNotDivisibleByTwo(counts) {
return Object.keys(counts).find(key => counts[key] % 2 !== 0);
}
这个解决方案给了我55%的支持,并不是很好。有解决这个问题的更好方法吗?