Advertisement
Guest User

Untitled

a guest
Sep 26th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const array = [3, 'a', 'a', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3];
  2.  
  3. const getMostFrequentValue = array => {
  4.   const countAllValues = array.reduce((acc, curr) => {
  5.     if (curr in acc) {
  6.       return { ...acc, [curr]: acc[curr] + 1 }
  7.     }
  8.  
  9.     return { ...acc, [curr]: 1 }
  10.   }, {})
  11.   const countedValues = Object.entries(countAllValues);
  12.   return countedValues.reduce((acc, [key, value], idx) => {
  13.     if (value === countAllValues[key]) {
  14.       return `item ${key} ${value} times`;
  15.     }
  16.     return acc;
  17.   }, {});
  18. }
  19.  
  20. console.log(getMostFrequentValue(array));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement