DEV Community

Nirmal Krishna
Nirmal Krishna

Posted on

3 2

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)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay