Hello everyone,
I’m actually doing one of the algorithm challenges and I have spend a lot of time trying to think how to get the array with the major ocurrence of a number. I have used pen and paper to draw my solutions for the challenge as a whole (I have completed many steps except this one).
For example, I’m showing the code portion related to what I’m asking for:
for(let n = 0; n < allNumbers.length; n++){
div = 2;
tempN = allNumbers[n];
for(let m = 0; m < allNumbers.length; m++){
while(tempN !== 1) {
if(tempN < div) {
div = tempN;
}
if(tempN % div !== 0) {
div += 1;
}
if(tempN % div == 0){
tempN = tempN / div;
nArray[n].push(div);
}
}
}
}
It prints out : [ [], [ 2 ], [ 3 ], [ 2, 2 ], [ 5 ] ]
As you can see, the number 2
is in two arrays, but I’m only want to keep the array with the highest occurrence of that number which is the one located at index 3
so that the output shows: [ [], [], [ 3 ], [ 2, 2 ], [ 5 ] ]
and discard or set the other to nothing (or if the occurrence is the same, to take just one of them).
Similarly, other examples could be:
From [[1, 2, 4], [2, 2, 3], [5], [5, 2], [4, 5]]
to have a result like this: [[1,4], [2, 2, 3], [], [], [4, 5]]
Thank you in advance!