1

Is there a possibility to restart this loop with a new index number:

    let ar = [1, 1, 2, 1, 2, 1, 3, 2, 3, 1];
    let sortedArray = ar.sort();

    let sameNumbersArray = [];
    let numberOfSameNumbers = 0;

    let lastIndexNumber = 0;

    for (i = lastIndexNumber; i < sortedArray.length; i++) {
      if (sortedArray[i] == sortedArray[i + 1]) {
        const sameNumber = sortedArray[i];
        sameNumbersArray.push(sameNumber);
      } else {
        break;
      }

      let lastIndexFromNumberArray = [];
      lastIndexFromNumberArray.push(sameNumbersArray.length);
      lastIndexFromNumberArray.push(3);
      lastIndexFromNumberArray.push(2);


      lastIndexNumber = lastIndexFromNumberArray.reduce(function (a, b) {
        return a + b;
      }, 0);

So basically that the loop (lastIndexNumber) starts with index[0], but then restarts with index[5] and index[7].

How would one add this extra loop?

FilipZafran
  • 315
  • 1
  • 5
  • 14

2 Answers2

1

I'm not 100% clear on the aim here. Are you able to elaborate on the desired result of the above?

It looks like you want to get an array of the unique numbers and perhaps the number of unique numbers from the source array?

If so, here's another way which might be cleaner:

let ar = [1, 1, 2, 1, 2, 1, 3, 2, 3, 1];
let sortedArray = ar.sort();

let newSameNumbersArray = unique(sortedArray);
//array of unique numbers:
console.log(newSameNumbersArray);
//count of unique numbers:
console.log(newSameNumbersArray.length);

function unique(array) {
    return Array.from(new Set(array));
}

This is based on this answer: https://stackoverflow.com/a/44405494/4801692

That said, you can directly set the value of i and use continue to move to the 'next' iteration.

i = 5;
continue;

This is bad though as you are in danger of feeding i a lower number and getting stuck in an infinite loop. If you can explain the requirement a little more I might be able to suggest something better.

Kohaku
  • 39
  • 6
  • 1
    Thanks for a very elaborate response. Please note that I am a full newby. I checked out your 'Set' suggestion - unfortunately I dont think it works, but this is cause I didnt explain well... I didnt want to put the whole code, as its very long and confusing. I have the following challenge: https://www.hackerrank.com/challenges/sock-merchant/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=warmup – FilipZafran Apr 25 '20 at 21:10
  • So I need to find pairs of numbers. My thought is to count all the 1s, then 2s, 3s... and divide them by 2 and I will get my result. But I need some how to restart the loop with the different starting point, depending on where it stopped... Not sure if this helps...? – FilipZafran Apr 25 '20 at 21:12
0

you can do such thing to find the pairs

let ar = [10, 10, 10, 20, 20, 10 ,30, 20 ,30]

function findPair(ar) {
        let counts = {};
        let count = [];
        let sum = 0;

for(let i = 0; i < ar.length; i++){
    let item = ar[i]
     counts[item] = counts[item] >= 1 ? counts[item] + 1 : 1;
}
count = Object.values(counts);
for(let i = 0; i < count.length; i++){
    if(count[i] >= 2){
        sum += Math.floor(count[i]/2)
    }
}
console.log(sum)
}

findPair(ar)
  • Thanks, you did solve it. Its just that I am not able to follow the code... I need to learn about objects and the logics behind... ? : Thanks for your work! – FilipZafran Apr 29 '20 at 13:26