group_by and its equivalent Map

The search for the rarest Pepe was dangerous but rewarding.

The Pepe meme is a popular but strange craze which has even led to the rise of a new religion: Kekism or the Cult of Kek. The challenge in the latest codewars kata set by the ubiquitous Giacomo Sorbi at his excellent Katathon meet up in CitizenM was to find the rarest Pepe in an array. Our function had to return the rarest Pepe in the list. If two or more pepes were equally rare we had to return a list of those pepes, sorted in alphabetical order. If the rarest pepe (or pepes) had a frequency of 5 or more then the function should return ‘No rare pepes!’.

The solution in javascript is the following. Pablo, a fellow Makers student, helped explain the code:

As Pablo put it: “In Javascript Map works like an object. It is not exactly an Object, but it is very similar.” Pablo then explained that we “then loop through the pepes array to populate the map object. In the map object the pepe string will be the key and the count of each object will be the value.” We loop through the pepes array to populate the map object. As Pablo put it: “In the map object the pepe string will be the key and the count of each object will be the value.” Pablo then continues to explain the for loop. As he he points out “For example the first element in the pepes array is ‘Go Pepe’ and we want to check if ‘Go Pepe’ is in our empty Map : it is undefined at the start. Then the map method allows us to get the element i in the map, but when our Map is empty, the value outputs ‘undefined’”. We then set pepe[i] as the key and count as the value in the Map objet. we then get the value of each key and save them in an array. We then loop through the array to retrieve only the pepes with the smallest values and save in a new Array : newArr. If the minvalue is more than 5 we must output that there is no rare pepe. Alternatively, if minValue < 5 then we use a ternary operator to give a sorted array if there is more than one or a string if there is only one pepe in the array.

Another shorter Javascript solution for the pepe problem gave more opportunity for exploration of the main points:

This also created a Map object much like a hash in ruby. The map was populated by the method forEach. Each element (pepe) of the array was mapped over in order to give the pepe as the key of the hash (or Map) and the number of that element as the value. Using a for loop over the key and value of the map hash the rarest pepe is then discovered. Finally the two if statements at the end of the solution output either “No rare pepes!” or a sorted version of the final array of rarest pepes.

In javascript the solution for finding the correct amount of pepes was Map which created a new hash. In Ruby the solution is slightly more attractive if no less difficult to understand:

We use the group_by to group the hash by result of a block. This returns a hash where the keys are the evaluated result from the block and the values are arrays of elements in the collection that correspond to the key. The code in the first line is quite clever. Firstly, the key and value of the hash are stated: count (the key), rare_pepes (the value). We then run uniq on the pepes array to increase the performance of the block and avoid looking for the same element twice. I preferred the ruby answer but through Pablo’s work I was starting to understand the workings of javascript.

Like what you read? Give Tom Spencer a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.