Count Elements With Strictly Smaller and Greater Elements LeetCode solution in JavaScript

1
var countElements = function (nums) {
2
let map = {},
3
total = 0;
4

5
// adding elements to map
6
for (let i of nums) map[i] ? map[i]++ : (map[i] = 1);
7

8
// Removing repeated elements
9
let newNums = [...new Set(nums)];
10

11
// If length of array after removing repeated nums is less than three return 0;
12
if (newNums.length < 3) return 0;
13

14
// sort the newNums array and remove the first and last element.
15
// for all the remaining elements check their number in map and add it to total variable
16
newNums
17
.sort((a, b) => a - b)
18
.slice(1, newNums.length - 1)
19
.forEach((num) => (total += map[num]));
20

21
// return total variable
22
return total;
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0