-3

Given an array of elements e.g. [9, 9, 9, 2, 5], how can I create a map of element and their frequency in the array [9=>3, 2=>1, 5=>1] using higher order function of array such as map, filter and reduce.

Mukesh Kumar
  • 783
  • 1
  • 9
  • 24
  • 2
    Your efforts so far ? – Code Maniac Mar 14 '19 at 16:55
  • 1
    @MukeshKumar not really, but people here needs minimum efforts from OP. and without any specific problem you can expect answer from people as this is not free coding service site. people here invest their time to help others but you must atleast show minimum effiorts – Code Maniac Mar 14 '19 at 17:03

1 Answers1

0

The idea is same as other answer but it doesn't print right map so I am posting correct one.

let arr =  [9, 9, 9, 2, 5];
let res = arr.reduce((acc, cur) => (!acc[cur] ? acc[cur] = 1 : acc[cur]++, acc), {})
console.log(res)
wang
  • 1,660
  • 9
  • 20