0

I have the following code

let range = [1,2,3];
let multiples = [1,2,3,4,5,6,2,4,6,3,6];

I want to find the first number in the multiples array that occurs range.lenght times (3);

I want to start with multiples[0] check how many times it occurs in multiples, if it occurs 3 times I want to return multiples[0], if it is less than 3 times, I want to check how many times multiples[1] occurs in the multiples array. If multiples[1] occurs 3 times I want to return multiples[1], else I move on to check multiples[2], etc. until I find a number that occurs 3 times. In the code above I should return 6.

I've looked at How to count the number of certain element in an array? and Idiomatically find the number of occurrences a given value has in an array and get closest number out of array among other research but have not figured it out yet.

I tried to simplify the question as much as possible. But if more info is needed it relates to this challenge on freeCodeCamp. Where I am at with my code is

function smallestCommons(arr) {
  let sortArr = arr.sort((a, b) => a - b);
  console.log(sortArr);
  let range = [];
  for (let i = sortArr[0]; i <= sortArr[1]; i++) {
    range.push(i);
  }
  console.log("range = " + range);
  let maxNum = range.reduce( (a, b) => a * b);
  console.log("maxNum = " + maxNum);

  let multiples = [];
  for (let i = 0; i < maxNum; i++) {

    let j = 0;
    do {
      multiples.push(j + range[i]);
      j += range[i];
    } while (j < maxNum);
    //j = 0;

  }
  for (let i = 0; i < multiples.length; i++) {
    let numberToFind = multiples[i];
   /*stuck here hence my question, maybe I shouldn't even start with a for loop*/ 
   //tried reduce, forEach, filter, while loop, do while loop 
  }
  console.log("multiples = " + multiples);


 }
console.log(smallestCommons([1,3]));

The logs are

1,3
range = 1,2,3
maxNum = 6
multiples = 1,2,3,4,5,6,2,4,6,3,6,NaN,NaN,NaN
eoja
  • 1,642
  • 2
  • 12
  • 21

2 Answers2

0

What you can do is, first split your string with , and then using below function loop for check.

function countLength(arr, checkNumber) {
    var count = 0;
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] === checkNumber) {
            count++;
        }
    }
    return count;
}

countLength(list, NUMBER YOU WANT TO CHECK);

And if you want to check first number occur for 3 time then you need to make change in function and introduce .map or .filter in action to count number.

Example

const multiples = [1,2,3,4,5,6,2,4,6,3,6];
let occurance_arr=[]; 

const aCount =  [...new Set(multiples)].map(x => {
    if(multiples.filter(y=> y==x).length == 3) {
        occurance_arr.push(x);
    }
});

console.log(occurance_arr);

Above code will give you 6 in console, if you have multiple value then 0th element is the answer you are looking for which is first three time occurrence of item.

Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
0

You can loop through your list keeping an object that maps each number to the number of times you've seen it. You can check the counts object as you loop, so if you see a number and the count is one less than your target, you can return it. If you make it through the loop without returning you didn't find what you're looking for — return something sensible :

let range = [1,2,3]
let multiples = [1,2,3,4,5,6,2,4,6,3,6]

function findFirstMult(arr, len){
    let counts = {}                         // to keep track of how many times you've seen something
    for (let n of arr){                     // loop throught the array
        if (!counts[n]) counts[n] = 0       // if it's then first time you've seen n, defined that key
        if (counts[n] == len - 1) return n  // found it
        counts[n] +=1                       // otherwise increase the count
    }
    return undefined
}

console.log(findFirstMult(multiples, range.length))

This will require only one loop through the array in the worse case and will return early if if finds something.

Mark
  • 90,562
  • 7
  • 108
  • 148