Created
April 2, 2020 05:24
-
-
Save javi-aire/49fbe5fe240d33952e9c245194872e89 to your computer and use it in GitHub Desktop.
Challenge 1 of 30 on LeetCode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let singleNumber = function(nums) { | |
// frequency counter | |
let freq = {}; | |
let lonely; | |
// iterate thru array | |
for(let num of nums){ | |
// if num does not exist as key, | |
// create it or increment otherwise | |
if(!freq[num]){ | |
freq[num] = 1; | |
} else { | |
freq[num] = (freq[num] || 0) + 1; | |
} | |
} | |
// Find key (num) with val === 1 | |
for(let key in freq){ | |
if(freq[key] === 1) { | |
lonely = parseInt(key); | |
} | |
} | |
// return that number | |
return lonely | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment