본문 바로가기

리트코드/easy

[리트코드] 136. Single Number - js

반응형

1. 문제

https://leetcode.com/problems/single-number/submissions/

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

2. 코드

/**
 * @param {number[]} nums
 * @return {number}
 */
var singleNumber = function(nums) {
    let map = new Map();

    for(let i=0;i<nums.length;i++){
        map.set(nums[i], (map.get(nums[i]) || 0) + 1);
    }
    
    for(let [k,v] of map){
        if(v === 1) return k;
    }
};
반응형