Created
April 7, 2020 23:07
-
-
Save javi-aire/fef293e0c2182be87ead96f5a51b7ffc to your computer and use it in GitHub Desktop.
Problem 7/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 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