DEV Community

Karleb
Karleb

Posted on

#260. Single Number III

https://leetcode.com/problems/single-number-iii/description/?envType=daily-question&envId=2024-05-31

/**
 * @param {number[]} nums
 * @return {number[]}
 */
var singleNumber = function (nums) {
    if (nums.length === 2) return nums

    const map = new Map()
    const res = []

    for (let num of nums) {
        map.set(num, (map.get(num) || 0) + 1);
    }

    for (let [key, value] of map) {
        if (value === 1) {
            res.push(
                key
            )
        }
    }

    return res
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)