Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,038 questions

40,790 answers

573 users

How to find elements that appear more than array_size/K times in an array with JavaScript

1 Answer

0 votes
function elements_that_appear_more_than_x_times(array, k) {
    let size = arr.length;
    let times = parseInt(size / k);
    console.log("more than " + times + " times");
    
    return array.filter((a, index) =>
        array.indexOf(a) === index &&
        array.reduce((accumulator, b) => +(a === b) + accumulator, 0) > times
    );
}

let k = 4;
let arr = [4, 8, 6, 5, 5, 8, 3, 2, 1, 2, 2, 5, 5, 5, 5, 8, 9, 8, 8];

// 8 appear 5 times, 5 appear 6 times

console.log(elements_that_appear_more_than_x_times(arr, k));



/*
run:

more than 4 times
[ 8, 5 ]

*/

 





answered Feb 11 by avibootz
...