Last active
April 3, 2020 06:46
-
-
Save javi-aire/6e18c825efea035bda4ec1de0dd5e795 to your computer and use it in GitHub Desktop.
Problem 1/30 of LeetCode 30-day challenge
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 way | |
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