Highest Frequency
In this problem, we'll learn how to keep a count of values as we go through an array. From that data, we'll return the most common string in the array.
We'll cover the following...
Highest Frequency
Instructions
Write a function that takes an array of strings and returns the most commonly occurring string in that array.
If there are multiple strings with the same high frequency, return the one that finishes its occurrences first, while going left to right through the array.
Input: Array of Strings
Output: String
Press + to interact
function highestFrequency(strings) {// Your code here}
Solution
Press + to interact
function highestFrequency(strings) {const frequencies = {};let maxFrequency = 0;let mostFrequentString = strings[0];for(let i = 0; i < strings.length; i++) {const thisStr = strings[i];if(frequencies[thisStr] === undefined) {frequencies[thisStr] = 1;} else {frequencies[thisStr]++;}if(frequencies[thisStr] > maxFrequency) {maxFrequency = frequencies[thisStr];mostFrequentString = thisStr;}}return mostFrequentString;}
How it Works
Variables
The three ...