0

so im inserting into dropdown list- list of names. I want to find out what name displayed the Most in the dropdown list. For exmaple: i have dropdownlist that includes: John Betty John John James

  • How can i write function that returns me the most displayed name? (John). Thank you! :)
  • 1
    Gather your names from the dropdown in an array, and then find the most common element in the array. Here are some ideas on how to find the most common element in an array in js: https://stackoverflow.com/questions/1053843/get-the-element-with-the-highest-occurrence-in-an-array – ee-4-me Aug 31 '23 at 14:19
  • Please share the code involved – Nico Haase Sep 01 '23 at 07:43

1 Answers1

0
function mode(arr){
return arr.sort((a,b) =>
      arr.filter(v => v===a).length
    - arr.filter(v => v===b).length
).pop();

}

mode(['pear', 'apple', 'orange', 'apple']); // apple

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 01 '23 at 09:26