2

I have a very simple array like this:

array = [1, 1, 6, 7, 9, 6, 4, 5, 4];

I need to be able to remove a value, but I need to remove only one value if there's duplicate values. So if I remove the value 6, the array should become:

array = [1, 1, 7, 9, 6, 4, 5, 4];

The order of which one gets removed doesn't matter, so it could be the last no. 6 or the first no. 6. How can I do this?

Edit I see there's a lot of confusion about why I need this, which results in incorrect answers. I'm making a Sudoku game and when a user inserts a number in a cell, the game has to check if the chosen number already occupies space in the same row or column. If so, the number of that specific row/column is added to this array. However, when a user fixes a mistake, the number of the row/column should be removed. A user can, however, make multiple mistakes in the same row or column, which is why I need to retain the duplicates in the array. Otherwise, users can make multiple mistakes in a row/column, and only fix one, and then the code will think there are no errors whatsoever anymore.

Hope this makes things more clear.

18
  • 1
    Possible duplicate of How do I make an array with unique elements (i.e. remove duplicates)? Commented Dec 29, 2016 at 14:53
  • 2
    I don't want to remove duplicates, I want to RETAIN them. Commented Dec 29, 2016 at 14:54
  • 2
    why is 1 still duplicated? Commented Dec 29, 2016 at 14:55
  • And what is criteria for removing/keeping? Why '1' isn't removed? Ah, you will provide element as param... Commented Dec 29, 2016 at 14:55
  • 1
    @erol_smsr, I think that answers bellow aren't right. First, var should be checked - if unique - keep it, if duplicate - remove it (but just one occurence), right? Commented Dec 29, 2016 at 15:01

4 Answers 4

4

Try to get the index of your item with indexOf() and then call splice()

let array = [1, 1, 6, 7, 9, 6, 4, 5, 4];
let index = array.indexOf(6);
array.splice(index,1);
console.log(array);

Sign up to request clarification or add additional context in comments.

3 Comments

Can I remove an item by its value instead of its index?
@erol_smsr no you can't. You need to get the index and remove by index
That's a very weak, static solution. Look at the Kukkuz's code.
3

var array=[1, 1, 6, 7, 9, 6, 4, 5, 4],
    removeFirst=function(val,array){
      array.splice(array.indexOf(val),1)
        return array;
    };

console.log(removeFirst(6,array));

4 Comments

@erol_smsr if I may, this does not check if it has a duplicate before removing the element?
Yeah it doesn't check it, but does it have to? I tested it in the console and the result is exactly what I need: var array = [1,2,7,7,9,10,9] undefined array.splice(array.indexOf(7), 1); [7] array [1, 2, 7, 9, 10, 9] As you can see, it removed one instance of number 7 from the array. You think the check has to be performed?
Yes I do think so, for instance for var array=[1, 1, 6, 7, 9, 6, 4, 5, 4] try removeFirst(7) and it will result in [1, 1, 6, 9, 6, 4, 5, 4]...
That's okay. If a number only occurs once in the array, it should also be removed. Thanks for double-checking though :)
3

You can use Array.prototype.findIndex to find the first index at which the element to be removed appears and then splice it.

Also you can create a hastable to ascertain that we remove only if a duplicate is availabe - see demo below:

var array = [1, 1, 6, 7, 9, 6, 4, 5, 4];

var hash = array.reduce(function(p,c){
  p[c] = (p[c] || 0) + 1;
  return p;
},{});

function remove(el) {
  if(hash[el] < 2)
     return;
  array.splice(array.findIndex(function(e) {
    return e == el;
  }), 1);
}

remove(6);
remove(7);
console.log(array);

Comments

1

If order of removed element (not elements!) isn't important, you can use something like this:

array = [1, 1, 6, 7, 9, 6, 4, 5, 4];
function remove_if_dupe(elem, array) {
dupes=[];
for(i=0;i<array.length;i++) {
if(array[i] === elem) {
dupes.push(elem);
}

}
if(dupes.length>1) {
//is duplicated
array.splice(array.indexOf(elem), 1);
}
return array;
}

console.log(remove_if_dupe(6,array));

This should keep unique elements, hopefully.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.