4

I have an array ['2530491','2530491','2530491','2530492'] the 2530491 is duplicated thrice, and I want to remove a single value of 2530491 from 3 of them, so the output would be like : ['2530491','2530491','2530492'].

fileArrayAnnounce_size = jQuery.grep(fileArrayAnnounce_size, function(value){
  return value != file_size.metas[0].size;
});

I try grip but it removes all value which same. I want to remove only a single value from duplicates.

8
  • stackoverflow.com/questions/18008025/… Commented Apr 20, 2017 at 14:25
  • what are the rules exactly....allow 2 duplicates but no more? Commented Apr 20, 2017 at 14:26
  • @charlietfl that seems to be the case. Twice but not thrice. Commented Apr 20, 2017 at 14:27
  • Even more than 2 duplicates as long as I removed a single of it. Just to remove single value from duplicate regardless how many duplicates Commented Apr 20, 2017 at 14:28
  • So if there are four of the same, you still only wan't to remove one item? May you clarify the rules? Commented Apr 20, 2017 at 14:28

3 Answers 3

5

You can use splice and indexOf to remove the first instance:

fileArrayAnnounce_size.splice(fileArrayAnnounce_size.indexOf('2530491'), 1)

A safer way:

var index = fileArrayAnnounce_size.indexOf('2530491')
if (index > -1) {
    fileArrayAnnounce_size.splice(index, 1);
}

Check and remove duplicates:

var mapOfValues = fileArrayAnnounce_size.reduce(function(vals, current) {
    if (vals[current]) {
        vals[current]++;
    } else {
        vals[current] = 1;
    }

    return vals;
}, {});

And now check and remove anything with more than 1 value:

for (var value in mapOfValues) {
    if (mapOfValues[value] > 1) {
        var idx = fileArrayAnnounce_size.indexOf(value);
        fileArrayAnnounce_size.splice(idx, 1);
    }
}

Demo: https://jsfiddle.net/1277mxt9/

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

7 Comments

Don't you also need a check as to whether a value has a duplicate? Otherwise all values would be removed.
don't see how this verifies a duplicate exists
I mean, this works but only if you know the duplicate values.
Updated the code to find duplicates or more, and then a way to remove 1 of each of those values
Pretty sure the edit above works fine @evolutionxbox
|
2

You could check if given element has dupe elements or not, if so - remove just one duplicate entry of specified element from the original array.

var arr = ['2530491','2530491','2530491','2530492'],
    hash = [...new Set(arr)];
    hash.forEach((v,i) => arr.indexOf(v) != arr.lastIndexOf(v) ? arr.splice(arr.indexOf(v), 1) : null);
    
    console.log(arr);

1 Comment

I like the simplicity
1

You can use filter() and pass one object as thisArg parameter to use it as hash table.

var data = ['2530491','2530491','2530491','2530492', '2530492'];

var result = data.filter(function(e) {
  if(!this[e]) this[e] = 1
  else if (this[e] == 1) return this[e] = 2, false
  return true;
}, {})

console.log(result)

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.