2

I'm trying to only remove one of the 2s from an array, but my code removes all of them. My code is as follows:

var arr = [2,7,9,5,2]
arr.filter(item => ((item !== 2)));

and:

var arr = [2,7,9,2,2,5,2]
arr.filter(item => ((item !== 2)));

Both remove all the 2s. I thought about removing duplicates, where it works if there's only one duplicate - e.g.:

Array.from(new Set([2,7,9,5,2]));
function uniq(a) {
 return Array.from(new Set(a)) 
}

But fails if there's multiple duplicates as it just removes them all, including any other duplicated numbers:

Array.from(new Set([2,7,9,9,2,2,5,2]));
function uniq(a) {
 return Array.from(new Set(a)) 
}

Does anyone know how to only remove one of the 2s? Thanks for any help here.

user8758206
  • 1,468
  • 8
  • 21
  • 1
    create a simple loop, when found an index with value of 2: `.splice(idx, 1)` this index and `break` the loop – Calvin Nunes Nov 19 '18 at 10:34
  • 1
    Which one do you want to remove ? The first `2`, or last, or ... – Mihai Alexandru-Ionut Nov 19 '18 at 10:34
  • 1
    Possible duplicate of [How do I remove a particular element from an array in JavaScript?](https://stackoverflow.com/questions/5767325/how-do-i-remove-a-particular-element-from-an-array-in-javascript) – Haroldo_OK Nov 19 '18 at 10:38

6 Answers6

5

You could use indexOf method in combination with splice.

var arr = [2,7,9,5,2]
var idx = arr.indexOf(2)
if (idx >= 0) {
    arr.splice(idx, 1);
}
console.log(arr);
Haroldo_OK
  • 5,235
  • 3
  • 31
  • 61
Mihai Alexandru-Ionut
  • 41,021
  • 10
  • 77
  • 103
  • Close, but it won't work correctly if the number isn't present on the array; checking if `indexOf` is non-negative would make it more robust. – Haroldo_OK Nov 19 '18 at 10:47
2

There are various ways to do that; one relatively simple way would be to use indexOf; see this other post: https://stackoverflow.com/a/5767357/679240

var array = [2, 7, 9, 5, 2];
console.log(array)
var index = array.indexOf(2);
if (index > -1) {
  array.splice(index, 1);
}
// array = [7, 9, 5, 2]
console.log(array);
Haroldo_OK
  • 5,235
  • 3
  • 31
  • 61
2

You could take a closure with a counter and remove only the first 2.

var array = [2, 7, 9, 2, 3, 2, 5, 2],
    result = array.filter((i => v => v !== 2 || --i)(1));
    
console.log(result);

For any other 2, you could adjust the start value for decrementing.

var array = [2, 7, 9, 2, 3, 2, 5, 2],
    result = array.filter((i => v => v !== 2 || --i)(2));
    
console.log(result);
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324
  • interesting answer. What does the v and (1) and (2) represent? I'm new to JS and haven't seen this type of syntax before - can you link to anything I can read to learn this? – user8758206 Nov 19 '18 at 16:44
  • 1
    one is to remove the first occurence of `2` and two for the second, and so on. it works, because the number gets decremented and if zero, the value is filterd out. the technique's name is closure with an IIFE (immediately-invoked function expression). – Nina Scholz Nov 19 '18 at 17:00
1

You can do:

const arr = [2, 7, 9, 2, 2, 5, 2];
const result = arr
  .reduce((a, c) => {
    a.temp[c] = ++a.temp[c] || 1;
    if (a.temp[c] !== 2) {
      a.array.push(c);
    }
    return a;
  }, {temp: {}, array: []})
  .array;

console.log(result);
Yosvel Quintero Arguelles
  • 15,149
  • 4
  • 33
  • 35
1

you can follow the following method

var arr= [2,3,4,2,4,5];
var unique = [];
$.each(arr, function(i, el){
    if($.inArray(el, unique) === -1) unique.push(el);
})
Tijo John
  • 616
  • 1
  • 8
  • 19
1

Most simple way to filter all duplicates from array:

arr.filter((item, position) => arr.indexOf(item) === position)

This method skip element if another element with the same value already exist.

If you need to filter only first duplicate, you can use additional bool key:

arr.filter((item, position) => {
  if (!already && arr.indexOf(item) !== position) {
    already = true
    return false
  } else return true
})

But this method have overheaded. Smartest way is use for loop:

for (let i = 0; i < arr.length; i++) {
  if (arr.indexOf(arr[i]) !== i) {
    arr.splice(i,1);
    break;
  }
}
Xeelley
  • 41
  • 5