Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Count frequency of array elements js

var arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4]

const map = arr.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());

console.info([...map.keys()]) // to get unique elements
console.info([...map.values()]) // to get the occurrences
console.info([...map.entries()]) // to get the pairs [element, frequency]
Comment

PREVIOUS NEXT
Code Example
Javascript :: regular expression for thousand separator 
Javascript :: js get element by attribute 
Javascript :: add params to url vue 
Javascript :: regex expression for password validation form validation 
Javascript :: npm adm-zip 
Javascript :: inner html jquery 
Javascript :: iterate over list array in solidity 
Javascript :: How to Use the toUpperCase() String Method in javascript 
Javascript :: remove double quotes from json array javascript 
Javascript :: javascript for...of loop to iterate over the multidimensional array 
Javascript :: how to slice/trim/remove last character in string 
Javascript :: react conditionally disable button 
Javascript :: Animated: `useNativeDriver` was not specified. This is a required option and must be explicitly set to `true` or `false` 
Javascript :: Date gethours js 
Javascript :: javascript Inserting values in between an array 
Javascript :: local storege in jquery 
Javascript :: append row javascript 
Javascript :: app.use public 
Javascript :: extract payload of expired jwt token in js 
Javascript :: ERESOLVE unable to resolve dependency tree npm ERR npm ERR! Found: @angular/core@5.0.3 npm ERR! node_modules/@angular/core 
Javascript :: get client id socket io 
Javascript :: express req body undefined 
Javascript :: iterate over array of objects javascript 
Javascript :: Obtain smallest value from array of objects in Javascript 
Javascript :: javascript indexof 
Javascript :: js exit function 
Javascript :: chrome extension open new tab from popup 
Javascript :: Error: Unable to resolve module ./index from 
Javascript :: read file in nodejs using fs 
Javascript :: javascript determine if string is valid url 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =