Disclaimer: This post assumes the array input will be composed of only positive numbers and there will only be one mode per input (i.e., there will only be one most repeating number).
The findMode Function
function findMode(array) {
// This function starts by creating an object where the keys are each unique number of the array and the values are the amount of times that number appears in the array.
let object = {}
for (let i = 0; i < array.length; i++) {
if (object[array[i]]) {
// increment existing key's value
object[array[i]] += 1
} else {
// make a new key and set its value to 1
object[array[i]] = 1
}
}
// assign a value guaranteed to be smaller than any number in the array
let biggestValue = -1
let biggestValuesKey = -1
// finding the biggest value and its corresponding key
Object.keys(object).forEach(key => {
let value = object[key]
if (value > biggestValue) {
biggestValue = value
biggestValuesKey = key
}
})
return biggestValuesKey
}
Function Breakdown
At a high-level, the function:
- takes in an array
- makes an object where the keys are each unique number of the array and the values are the amount of times that number appears in the array
- finds the key that points to the biggest value
- returns that key.
Analyzing the findMode function from top to bottom, first we create an empty object and assign it to the "object" variable. Then, to fill up our object, we create a for
loop that goes through each member of the array. We want our object to end up looking something like this:
{1: 4, 2: 7, 9: 3}
Each key of the object is a unique number in the array and the key's value is the amount of times that number appears in the array. So in the above example object, the mode would be 2, because it appears the most in the array (7 times).
To get our object to look like that, we introduce an if...else
block. For each element in the array input, if the element is already a key in the object, we increment that key's value by one. If the element is not already in the object, we make that element a new key and set its value to one.
This is all taken care of in the following code from the findMode function:
if (object[array[i]]) {
// increment existing key's value
object[array[i]] += 1
} else {
// make a new key and set its value to 1
object[array[i]] = 1
}
Next, we declare two new variables, biggestValue
and biggestValuesKey
, and assign them both to -1. It doesn't matter which negative number you assign to these variables, it just has to be the guaranteed smallest number in the array.
Now we need to find the biggest value of our object and return that value's key (the mode). To do that, we receive a new array of our object's keys with Object.keys, passing in our object to the keys
method. Then, we iterate through that new array with a .forEach
enumerator. Next, we get a value from our object with object[key]
and assign it to the new value
variable. If that value is greater than the biggestValue
variable, our new biggestValue
is set to value
and our new biggestValuesKey
is set to key
. The code goes through those steps for each key in the array returned from Object.keys(object)
. Finally, we return biggestValuesKey
, which is the mode.
Thank you for reading. Please let me know if you have any questions, if there is a better way to find the mode or if I made a mistake somewhere in this post.
Top comments (0)