Minimum Cost to Move Chips to The Same Position LeetCode solution in JavaScript

1
var minCostToMoveChips = function (position) {
2
const hashMap = new Map();
3
let odd = 0,
4
even = 0;
5
for (const chip of position) {
6
if (hashMap.has(chip)) hashMap.set(chip, hashMap.get(chip) + 1);
7
else hashMap.set(chip, 1);
8
}
9
for (const [key, value] of hashMap) {
10
if (key % 2 === 0) even += value;
11
else odd += value;
12
}
13
return Math.min(odd, even);
14
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0