Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const array = [3, 'a', 'a', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3];
- const getMostFrequentValue = array => {
- const countAllValues = array.reduce((acc, curr) => {
- if (curr in acc) {
- return { ...acc, [curr]: acc[curr] + 1 }
- }
- return { ...acc, [curr]: 1 }
- }, {})
- const countedValues = Object.entries(countAllValues);
- return countedValues.reduce((acc, [key, value], idx) => {
- if (value === countAllValues[key]) {
- return `item ${key} ${value} times`;
- }
- return acc;
- }, {});
- }
- console.log(getMostFrequentValue(array));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement