티스토리 뷰

 

const alphabets = [1,1,1,2,3,3,1,4,5];
const counts = alphabets.reduce((acc, current) => {
    if(acc[current]){
        acc[current] += 1;
    } else {
        acc[current] = 1;
    }
    return acc;
}, {});

console.log(counts); //{ '1': 4, '2': 1, '3': 2, '4': 1, '5': 1 }

'Algorithms' 카테고리의 다른 글

find intersection algorithm  (0) 2021.03.28
two sum  (0) 2021.03.26
on sale products algorithm  (0) 2021.02.12
How many typos in a string  (0) 2021.02.12
CamelCase  (0) 2021.02.12