-1

Backset is an array of arrays, I am trying to filter out any arrays which contain a repetition of elements, such as an array with 2 ones. I would like to remove these from the Backset array.

However this is not happening and some arrays in backSet such as [1,2,2] stay in backSet.

for(z=0; z<backSet.length; z++){
    backSet[z].sort();
    tempBackSort = [];
    for(k=0; k< backSet[count].length; k++){
      if(tempBackSort.includes(backSet[count][k])){
        backSet.splice(backSet.indexOf(backSet[count]),1);
        kon = 0;
        continue;
      } else{
        tempBackSort.push(backSet[count][k]);
        kon = 1;
      }
    }

    if(kon===1){
      count++;
    }
    
    backSet[z].sort();
  }
  • @Ivar I thought I sorted out the skipping over index by using the kon variable, so the index only goes up if the array was not removed – Herblawunf Oct 13 '22 at 13:33
  • The kon variable is pointless as it does nothing in the loop – Jaromanda X Oct 13 '22 at 13:38
  • It's difficult to fully follow the logic without a [mcve]. But two things that stand out to me: You continue looping in the inner for-loop after you remove that item from `backSet`. And your `kon` variable is overwritten with every iteration of your loop, so you are effectively only checking the last element. – Ivar Oct 13 '22 at 13:40
  • @JaromandaX The reason which I added it is so that it does not skip over an index when it removes an element – Herblawunf Oct 13 '22 at 13:41
  • @Ivar does the continue; not break out of the for loop after the item is removed? – Herblawunf Oct 13 '22 at 13:42
  • [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – VLAZ Oct 13 '22 at 13:43
  • @Herblawunf No, `continue` continues the for-loop to the next iteration, skipping the rest of the body of the for-loop. (In your case it is the last statement that is executed in your body anyway, so it doesn't do anything.) `break` breaks the loop. – Ivar Oct 13 '22 at 13:44

1 Answers1

1

There are several issues:

  • Where you have continue now it accomplishes nothing. You need break, so the inner loop is exited

  • You try to overcome the problem of iterating over an array where elements are sometimes removed by using count, but:

    • count is not initialised
    • You still have code that works with backSet[z] instead of backSet[count]
    • It would have been easier to just decrease z when an element is removed, or even better: to let the outer loop iterate backwards from end to start -- then you don't need to adjust any index.
  • Variables are implicitly declared as globals: this is bad practice. Define your variables with let or const.

  • By sorting the inner arrays, you mutate the original data. This might be OK for your case, but still it is maybe surprising for code that depends on this piece of code.

  • Sorting is also not the most efficient way to find duplicates. Instead of pushing values in tempBackSort, put them in a Set (which cannot contain duplicates) and compare its size with the original array: if their sizes are not the same, then there are duplicates.

  • Instead of splicing inside a loop (which is the cause of your trouble), use the array method filter which returns the filtered array.

Here is how it could be coded:

backSet = backSet.filter(arr => (new Set(arr)).size == arr.length);
trincot
  • 317,000
  • 35
  • 244
  • 286