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

--

--

--

Always learning.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

https://youtube.com/watch?v=Efn4ijny6_0&feature=share

[Fix] TypeError [ERR_INVALID_ARG_TYPE]: The “path” argument must be of type string.

Web Scraping Tutorial #2 — Scrape Json data using python & beautifulsoup4 Library

How do you change the font in GUI Java?

Top Accounts

Check web server availability: using Go websocket server

Flipping an Image — LeetCode

Bubble Sort

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
Saul Feliz

Saul Feliz

Always learning.

More from Medium

Beginner’s guide to Linked List in JavaScript

Javascript Fundamentals: Function, Array, and Object — Part 1

Underestimating the Power of the Event Object

Coding Space Invaders in JavaScript Complete Tutorial Every Step Explained with HTML5 Canvas