Skip to content

Instantly share code, notes, and snippets.

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