group_by and its equivalent Map
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!’.
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.