To find the mode of all keys in a Map in JavaScript, we can follow these steps:
valuesArr
to store all the values of the Map.Map.forEach()
method and push each value to the valuesArr
.frequency
to store the frequency of each value in the valuesArr
.valuesArr
and add each value as a key to the frequency
object if it doesn't already exist, and increment the value of that key by 1.maxFreq
to store the maximum frequency of any value in the frequency
object.modes
to store all the modes of the values in the valuesArr
.frequency
object and if the value of any key is equal to maxFreq
, push that key to the modes
array.modes
array as the result, which contains all the modes of the keys in the Map.Here's the code:
index.tsx683 chars40 lines
gistlibby LogSnag