Most common

In this coding challenge, you will be implementing a function mostCommon(words) that takes an array of strings words as its argument and should return the string that appears the most often. If there are two or more elements that appear the same number of times, then the function should the element whose’ first occurrence has a smaller index.

Instructions

  • If the input array is empty then return null.
  • The input array can only contain strings and numbers.

Example test cases

const words = ["apple", "banana", "apple", "cherry", "banana", "apple"]; const mostCommonWord = mostCommon(words); console.log(mostCommonWord); // "apple"
const words = ["apple", "banana", "apple", "banana", "banana", "apple"]; const mostCommonWord = mostCommon(words); console.log(mostCommonWord); // "apple"
Adding your container request
Getting your dedicated container
Connecting to your container
Setting up your editor
Finalizing your playground
TerminalEditorBrowser