Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS: Finding unpaired elements in an array

Tags:

javascript

I have the following question (this is not school -- just code site practice questions) and I can't see what my solution is missing.

A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired.

Assume that:

  • *N is an odd integer within the range [1..1,000,000];
  • *each element of array A is an integer within the range [1..1,000,000,000];
  • *all but one of the values in A occur an even number of times.

EX: A = [9,3,9,3,9,7,9] Result: 7

The official solution is using the bitwise XOR operator :

function solution(A) {
    
    var agg = 0;
    
    for(var i=0; i<A.length; i++) {
        agg ^= A[i];
    }
    
    return agg;
}

My first instinct was to keep track of the occurrences of each value in a Map lookup table and returning the key whose only value appeared once.

function solution(A) {

    if (A.length < 1) {return 0}
    let map = new Map();
    let res = A[0]
    for (var x = 0; x < A.length; x++) {
        if (map.has(A[x])) {
            map.set(A[x], map.get(A[x]) + 1)
        } else {
            map.set(A[x], 1)
        }
    }
    for ([key,value] of map.entries()) {
        if (value===1) {
            res = key
        } 
    }

    return res;
}

I feel like I handled any edge cases but I'm still failing some tests and it's coming back with a 66% correct by the automated scorer.

like image 801
Jason Boyas Avatar asked Aug 21 '18 06:08

Jason Boyas


People also ask

How do you find unpaired elements in an array?

Naive Approach In the naive approach, we try to search the array linearly for the pair of each element. Once we find an element that doesn't have a pair, we declare it as the answer to the problem. loop. , then the current element was unpaired.

What is array find method in JavaScript?

JavaScript Array find() The find() method returns the value of the first element that passes a test. The find() method executes a function for each array element. The find() method returns undefined if no elements are found. The find() method does not execute the function for empty elements.

How do you remove all occurrences of an element from an array in JavaScript?

Use the Array. splice() method to remove all the elements from the original array. Use the Array. pop() method with a loop to remove all the elements of the original array.


2 Answers

You could use a Set and check if deletion deletes an item or not. If not, then add the value to the set.

function check(array) {
    var s = new Set;
    
    array.forEach(v => s.delete(v) || s.add(v));
    
    return s.values().next().value;
}

console.log(check([9, 3, 9, 7, 3, 9, 9])); // 7
like image 59
Nina Scholz Avatar answered Nov 14 '22 23:11

Nina Scholz


You're not accounting for cases like this:

[ 1, 1, 2, 2, 2 ] => the last 2 is left unpaired

So your condition should be if ( value % 2 ) instead of if ( value === 1 ).

I think also there is not much benefit to using a Map rather than just a plain object.

like image 27
We Are All Monica Avatar answered Nov 14 '22 23:11

We Are All Monica