7

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

 3 years ago
source link: https://dev.to/meuequalsd/finding-element-that-appears-once-in-an-array-where-other-elements-appear-twice-n91
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

Jul 1

・1 min read

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 modeExit fullscreen mode

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK