DEV Community

Cover image for Road to Genius: advanced #29
Ilya Nevolin
Ilya Nevolin

Posted on

Road to Genius: advanced #29

Each day I solve several coding challenges and puzzles from Codr's ranked mode. The goal is to reach genius rank, along the way I explain how I solve them. You do not need any programming background to get started, and you will learn a ton of new and interesting things as you go.

Here's our second challenge on the advanced level:

function mode(x) {
  let counts = {};
  let mode = [];
  let max = 0;
  let n = x.length;
  let i = -1;
  let d, k;
  while (++i < n) {
    d = x[i]
    if (d in counts)
      k = ++counts[d];
    else
      k = counts[d] = 1;
    if (k === max)
      mode.push(d);
    else if (k > max) {
      max = k;
      mode = [d];
    }
  }
  return mode;
}
let inp = [2,4,8,8,2,2]
let out = mode(inp)
let A = out.length

// A = ? (number)
Enter fullscreen mode Exit fullscreen mode

We need to determine the value of A, which is the size of the array out, which is the output of the function mode.

If you briefly analyze this function, you are unlikely to recognize it, unless you explicitly wrote something very similar in the past. The name of the function mode is a well-known operation in statistics for determining the most occurring numbers in a set. In other words, the mode of a set of numbers is the number with the highest frequency.

What this function basically does is keep track of the most occurring numbers, these are stored in the internal array mode, do not confuse it with the function mode.

The while-loop iterates over each number in the input array x, each element is assigned to variable d. The first if-else statement keeps track of all the numbers as a key-value object; whereby variable k is the number of occurrences of the number d.

The second if-else checks if k is greater than the current highest occurring number (i.e. its frequency). It uses the variable max to keep track of the highest frequency. If k == max then d is added to the mode array. Because it's among the highest occurring numbers up to that point.
Else if k > max; meaning the number d has a higher frequency than all the numbers in mode, then it overrides max with k, and mode becomes a new array with d as the first number.

Let's put this all in pseudo-code:

x = [2, 4, 8, 8, 2, 2]
mode = []
max = 0

-> d = 2
   k = 1
   max = 1
   mode = [2]

-> d = 4
   k = 1
   max = 1
   mode = [2, 4]

-> d = 8
   k = 1
   max = 1
   mode = [2, 4, 8]

-> d = 8
   k = 2
   max = 2
   mode = [8]

-> d = 2
   k = 2
   max = 2
   mode = [8, 2]

-> d = 2
   k = 3
   max = 3
   mode = [2]
return mode;


--> out = mode(x) = [2]
    A = out.length = 1
Enter fullscreen mode Exit fullscreen mode

coding challenge answer

By solving these challenges you train yourself to be a better programmer. You'll learn newer and better ways of analyzing, debugging and improving code. As a result you'll be more productive and valuable in business. Join me on the Road to Genius and upgrade your programming skills, at https://nevolin.be/codr/

Top comments (0)