Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push duplicate items into a separate array in Javascript with for loop?

For some reason, the manipulated doubleArray below is not shown in the console. Any variables that I declare after the for loop won't show to the console on both cases. Consider that in the first algorithm, there is only one for loop with x being incremented everytime. Whereas, in the second algorithm, it's a nested for loop. Can someone help me fix my error in both algorithms? First Algorithm:

var isDuplicate = function() {
  var helloWorld = [1,2,3,4,3];
  var doubleValue = [];
  var x = 0;
  for (i = 0; i < helloWorld.length; i++) {
    x = x + 1;
    if (helloWorld[i] === helloWorld[x] && i !== x) {
      doubleValue.push(helloWorld[i])
      console.log(helloWorld[i]);
    } else {
      continue;
    }
  }
  console.log(doubleValue);
};

The second Algorithm:

var isDuplicate = function() {
  var helloWorld = [1,2,3,4,3];
  var doubleValue = [];
  for (i = 0; i < helloWorld.length; i++) {
    for (x = 1; x < helloWorld.length; i++) {
      if (helloWorld[i] === helloWorld[x] && i !== x) {
        doubleValue.push(helloWorld[x]);
      }
    }
  }
  console.log(doubleValue);
};
like image 640
Shreejal Luitel Avatar asked Jun 04 '20 07:06

Shreejal Luitel


2 Answers

In first algorithm, you are only checking if the number at current index is equal to the number at the next index, meaning you are only comparing numbers at consecutive indexes. First algorithm will work only if you have duplicate numbers on consecutive indexes.

In second algorithm, you are incrementing i in both loops, increment x in nested loop, change x = 1 to x = i + 1 and your error will be fixed.

Here's the fixed second code snippet

var isDuplicate = function() {
  var helloWorld = [1,2,3,4,3, 1, 2];
  var doubleValue = [];
  for (let i = 0; i < helloWorld.length; i++) {
    for (let x = i + 1; x < helloWorld.length; x++) {
      if (helloWorld[i] === helloWorld[x] && i !== x) {
        doubleValue.push(helloWorld[x]);
      }
    }
  }
  console.log(doubleValue);
};

isDuplicate();

Heres's another way to find the duplicates in an array, using an object. Loop over the array, if current number is present as key in the object, push the current number in the doubleValue array otherwise add the current number as key-value pair in the object.

const isDuplicate = function() {
  const helloWorld = [1,2,3,4,3, 1, 2];
  const doubleValue = [];
  const obj = {};
  
  helloWorld.forEach(n => obj[n] ? doubleValue.push(n): obj[n] = n);
  
  console.log(doubleValue);
};

isDuplicate();
like image 100
Yousaf Avatar answered Nov 12 '22 17:11

Yousaf


Not entirely sure what you are trying to do. If you are only looking for a method to remove duplicates you can do the following:

const hello_world = [1, 2, 2, 3, 4, 5, 5];
const duplicates_removed = Array.from(new Set(hello_world));

A set is a data object that only allows you to store unique values so, when converting an array to a set it will automatically remove all duplicate values. In the example above we are creating a set from hello_world and converting it back to an array.

If you are looking for a function that can identify all the duplicates in an array you can try the following:

const hello_world = [1, 2, 2, 3, 4, 5, 5];
const duplicates_found = hello_world.filter((item, index) => hello_world.indexOf(item) != index);
like image 2
Nathan Cramer Avatar answered Nov 12 '22 18:11

Nathan Cramer