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.

erol_smsr
  • 1,454
  • 4
  • 24
  • 49
  • 1
    Possible duplicate of [How do I make an array with unique elements (i.e. remove duplicates)?](http://stackoverflow.com/questions/6940103/how-do-i-make-an-array-with-unique-elements-i-e-remove-duplicates) – CroMagnon Dec 29 '16 at 14:53
  • 2
    I don't want to remove duplicates, I want to RETAIN them. – erol_smsr Dec 29 '16 at 14:54
  • 2
    why is 1 still duplicated? – epascarello Dec 29 '16 at 14:55
  • And what is criteria for removing/keeping? Why '1' isn't removed? Ah, you will provide element as param... – sinisake Dec 29 '16 at 14:55
  • In your example, a 4 must disappear too, no ? – Aethyn Dec 29 '16 at 14:55
  • Check if exists; if yes remove, if still exists, you've done what you wanted, if not, return item to array. – Midnight_Blaze Dec 29 '16 at 14:55
  • Possible duplicate of [How to remove a particular element from an array in JavaScript?](http://stackoverflow.com/questions/5767325/how-to-remove-a-particular-element-from-an-array-in-javascript) – Heretic Monkey Dec 29 '16 at 14:56
  • Anyway, CroMagnon's link is still relevant in this particular case of use. If you want to retain duplicates just log it somewhere (array, console, log file...) – Aethyn Dec 29 '16 at 14:57
  • @MikeMcCaughan Can't you see that the difference between the questions is that I need to retain the duplicates? Please stop marking this as a duplicate if you don't understand the issue :) – erol_smsr Dec 29 '16 at 14:58
  • The question I linked to is not about removing duplicates. Please stop complaining about duplicates if you don't read the duplicate :). – Heretic Monkey Dec 29 '16 at 15:00
  • They are all just examples. So there could also be four `1`'s in the array, and two `8`'s. Point is, that when I need to remove an item with value `1`, I can just remove it without removing the duplicates. I chose the number `6` as an example only. – erol_smsr Dec 29 '16 at 15:01
  • 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? – sinisake Dec 29 '16 at 15:01
  • @sinisake Could you add that as an answer with an example please? I'd really appreciate it :) – erol_smsr Dec 29 '16 at 15:02
  • Question edited, please check it out if you don't understand why I need this. – erol_smsr Dec 29 '16 at 15:07
  • Downvoted because you didn't show the code you've used. Answers that just write the code are not as useful since we don't know what you were having a hard time with. – Ruan Mendes Dec 29 '16 at 15:10
  • @JuanMendes Exactly what part of my code did you want to see? There are questions which ask how to remove an item from an array in a regular way with the same format as my question (example array and description) and those have hundreds of upvotes. So you don't make any sense. If you can't help, please just stay away... – erol_smsr Dec 29 '16 at 15:12
  • @erol_smsr When you don't post what you have tried, it looks like "please do my work for me". When you do post your code, we can figure out where you went wrong. This is a simple question, if you did attempt something, it should all fit here. http://stackoverflow.com/help/how-to-ask and http://stackoverflow.com/help/mcve – Ruan Mendes Dec 29 '16 at 15:48
  • @JuanMendes I understand. I could maybe add a splice method or something which removes an item, but then I would 'force' people here to use my code as a reference. Most people here know way more than I, so I though I'd just leave it to the community in this case. Check out my other questions and you'll see I have questions with loads of code and things I've tried. – erol_smsr Dec 29 '16 at 15:57

4 Answers4

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);
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
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);
kukkuz
  • 41,512
  • 6
  • 59
  • 95
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));
LellisMoon
  • 4,810
  • 2
  • 12
  • 24
  • @erol_smsr if I may, this does not check if it has a duplicate before removing the element? – kukkuz Dec 29 '16 at 15:40
  • 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? – erol_smsr Dec 29 '16 at 15:42
  • 1
    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]`... – kukkuz Dec 29 '16 at 15:46
  • 1
    That's okay. If a number only occurs once in the array, it should also be removed. Thanks for double-checking though :) – erol_smsr Dec 29 '16 at 16:11
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.

sinisake
  • 11,240
  • 2
  • 19
  • 27