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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,957 questions

51,899 answers

573 users

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

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, 8, 6, 5, 5, 8, 3, 2, 1, 2, 2, 5, 5, 5, 5, 8, 9, 8, 8, 5];
 
// 8 appear 6 times, 5 appear 7 times
 
console.log(elements_that_appear_more_than_x_times(arr, k));
 
 
 
/*
run:
 
more than 5 times
[ 8, 5 ]
 
*/

 



answered Feb 11, 2024 by avibootz
...