Good afternoon, I have doubts to assemble this simple system, if anyone can help me with any ideas, thank you very much.
I have a basevector[0…500] numbers
I have an auxvector[1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41]
I need to read the 500 numbers in the basevector and count how many times 4 numbers in a row appear within the auxvector in the basevector.
1 Like
So there are a few ways to do it that strike to mind.
What have you tried?
1 Like
Thanks for the feedback friend, I managed to solve completely that way.
// function to count occurrences of elements in array2 in array1
function countOccurrences(array1, array2, num) {
let count = 0;
let contador_seis = 0;
for (let i = 0; i < array1.length; i++) {
if (array2.includes(array1[i])) {
count++;
if (count == num)
{
contador_seis++;
count = 0;
}
}
else
{
count = 0;
}
}
return contador_seis;
}
function relatorio(){
let black_count_um = 0;
black_count_um = countOccurrences(basevector, black_vector, 1);
if I want to look for other numbers of numbers I just need to change the value of the variable num and call it correctly.
That will accomplish the goal.
Here’s how I did it; perhaps a bit more complex than is strictly necessary (read: Someone can do it better than me
) , but…
//I'm generating some random values.
let mainarray = Array.from({length: 500}, () => Math.floor(Math.random() * 43))
//This is just "every odd number between 1 and 41"
let auxarray = Array.from({length: 20}, (i) => i*2+1))
//Create an array of 0/1's for the includes, so i dont have to do it more than once; also so i can use math.
let subarray = mainarray.map((x) => auxarray.includes(x) * 1)
//Generate the result array:
return subarray.map((lambda,i) => subarray.slice(i,i+4).reduce((a,b) => a+b) == 4 ? i : null).filter(n => n)
//or if you just want to know how many times, return the .length of the above.
1 Like