Skip to content

Instantly share code, notes, and snippets.

@javi-aire
Created April 7, 2020 23:07
Show Gist options
  • Select an option

  • Save javi-aire/fef293e0c2182be87ead96f5a51b7ffc to your computer and use it in GitHub Desktop.

Select an option

Save javi-aire/fef293e0c2182be87ead96f5a51b7ffc to your computer and use it in GitHub Desktop.
Problem 7/30 of LeetCode 30-day challenge
let countElements = function(arr) {
// returning a counter of times a number x + 1
// is found in an array
let count = 0;
// need storage to keep track of x + 1 frequency
let storage = {};
for(let num of arr){
// if a number x + 1 is in the array
if(arr.indexOf(num + 1) > -1) {
storage[num] = (storage[num] || 0) + 1;
}
}
for(let num of Object.values(storage)){
count += num;
}
return count;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment