-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathcountNumbers.js
More file actions
46 lines (33 loc) · 822 Bytes
/
countNumbers.js
File metadata and controls
46 lines (33 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* Count all the numbers in an array
*
* @param {number[]} arr - An array containing numbers
* @returns {object} - an object where the key is the number, and the value is the count of that number
*
* ex: countNumbers([1,1,1,2,2,3,4])
* returns { 1:3, 2:2, 3:1, 4:1 }
*/
function countNumbers(arr) {
const output = {};
arr.forEach(el => {
if (output[el]) {
output[el] += 1;
} else {
output[el] = 1;
}
console.log(output)
})
return output
}
// let count = {}
// for (let i = 0; i < arr.length; i++) {
// const el = arr[i];
// if (count[el]) {
// count[el] += 1
// } else {
// count[el] = 1;
// }
// }
// return count;
// }
module.exports = countNumbers