0

Good Day, I am trying to remove duplicate element from an array using the forEach loop. But at this point in time i get some errors. Below are my codes

function removeDup(arr) {
    let result = arr.forEach((item, index) => { if (index > 1) item.shift() });
    return result;
}

I am not even sure if this code would work for removing duplicates because when i run it in a browser console i get this error

if (index > 1) item.shift(); ^

TypeError: item.push is not a function

First of all, how do i fix this error and secondly would this code work for removing duplicates?

diagold
  • 493
  • 1
  • 7
  • 28

4 Answers4

5

You can try :

function removeDup(arr) {
  let result = []
  arr.forEach((item, index) => { if (arr.indexOf(item) == index) result.push(item) });
  return result;
}

Explanaton:

first initialize result with empty array. Then iterate through passed array, check if the index is its first occurance of item. push to result array. return result array.

Alternative solution :

function removeDup(arr) {
  let result = []
  arr.forEach((item, index) => { if (result.indexOf(item) === -1) result.push(item) });
  return result;
}

Explanation :

you can avoid checking for index as first occurance by checking if that element has already been pushed into result array or not.

binariedMe
  • 4,309
  • 1
  • 18
  • 34
2

function findDuplicates(data) {

  let result = [];

  data.forEach(function(element, index) {
    //Checks data.indexOf(1) which is 0 === 0, then push it to array
    //Now again when it checks the value of 1 which corresponds to a value of 1 but in forloop has an index of 5, so exits and duplicate values are not pushed
    if (data.indexOf(element) === index) {
      result.push(element)
    }
  });

  return result;
}

console.log(findDuplicates([1, 2, 3, 4, 5, 1, 2]))
arunmmanoharan
  • 2,535
  • 2
  • 29
  • 60
2

Why not use a Set? e.g.

var arr = ['a', 'b', 'a']                                                                               
var unique = Array.from(new Set(arr))                                                                   
console.log(unique) // ['a', 'b']
Tony Gentilcore
  • 193
  • 1
  • 11
1

You can use filter it will return a new array set as your checked condition

The indexOf() method searches the array for the specified item, and returns its position.

The search will start at the specified position, or at the beginning if no start position is specified, and end the search at the end of the array.

Returns -1 if the item is not found.

If the item is present more than once, the indexOf method returns the position of the first occurence.

function removeDup(arr) {
    let result = arr.filter(function (item, pos) {return arr.indexOf(item) == pos});  
    // at first loop -> item = 1 -> so indexOf = 0 & pos = 0 -> so return data
    // at second loop -> item = 4 -> so indexOf = 1 & pos = 1 -> so return data
    // at third loop -> item = 5 -> so indexOf = 2 & pos = 2 -> so return data
    // at fourth loop -> item = 4 -> so indexOf = 1 & pos = 3 -> no return
    // at fifth loop -> item = 1 -> so indexOf = 0 & pos = 4 -> no return
    return result;
}

var d = removeDup([1,4,5,4,1]);
console.log(d);
Community
  • 1
  • 1