Skip to content

Instantly share code, notes, and snippets.

@javi-aire
Created April 2, 2020 05:24
Show Gist options
  • Save javi-aire/49fbe5fe240d33952e9c245194872e89 to your computer and use it in GitHub Desktop.
Save javi-aire/49fbe5fe240d33952e9c245194872e89 to your computer and use it in GitHub Desktop.
Challenge 1 of 30 on LeetCode
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