DEV Community

Nirmal Krishna
Nirmal Krishna

Posted on

Finding element that appears once in an array where other elements appear twice : Leetcode

This is an example implementation using hashmap. The input array nums is considered to have only one unique number found once, other numbers occurs > once.

// ts
function singleNumber(nums: number[]): number {

    const hash = {};

    for(let i = 0; i< nums.length; i++){   
        hash[nums[i]] = hash[nums[i]] ? hash[nums[i]] + 1 : 1
    }

    return Object.keys(hash).filter(k=> hash[k] === 1).map(k=> parseInt(k))[0];

};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)