How to solve HackerRank’s Equalize The Array Problem (JavaScript)

function equalizeArray(arr) {
let arrObj = {};
let maxCount = 0;
for (let num of arr) {
arrObj[num] = arrObj[num] + 1 || 1;
}
for (let value in arrObj) {
if (arrObj[value] > maxCount) {
maxCount = arrObj[value];
}
}
return (arr.length - maxCount);}
  1. Draw it out; and do it manually as a human would
  2. Take those steps, and write it out, or pseudo code it into steps
  3. THEN begin translating that into code.
let arrObj = {};
for (let num of arr) {
arrObj[num] = arrObj[num] + 1 || 1;
}
arrObj = {
1:1,
2:1,
3:3
}
let maxCount = 0;
for (let value in arrObj) {
if (arrObj[value] > maxCount) {
maxCount = arrObj[value];
}
}
return (arr.length - maxCount);
that’s all she wrote

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store