Deleting both values from array if duplicate - JavaScript/jQuery

I have an array here:

var myArr = [1, 1, 2, 5, 5, 7, 8, 9, 9];

Now I want to remove both appearances of a duplicate. So the desired result is not:

var myArr = [1, 2, 5, 7, 8 ,9];

but

var myArr = [2, 7, 8];

Basically I know how to remove duplicates, but not in that that special way. Thats why any help would be really appreciated!

Please note: My array is filled with strings. The numbers here were only used as an example.

jsfiddle for this code:

var myArr = [1, 1, 2, 5, 5, 7, 8, 9, 9];
var newArr = myArr;
var h,i,j;


for(h = 0; h < myArr.length; h++) {
    var curItem = myArr[h];
    var foundCount = 0;
    // search array for item
    for(i = 0; i < myArr.length; i++) {
        if (myArr[i] == myArr[h])
            foundCount++;
    }
    if(foundCount > 1) {
        // remove repeated item from new array
        for(j = 0; j < newArr.length; j++) {
            if(newArr[j] == curItem) {                
                newArr.splice(j, 1);
                j--;
            }
        }            
    }
}

Answer: Use the indexOf () Method. You can use the indexOf () method in conjugation with the push () remove the duplicate values from an array or get all unique values from an array in JavaScript.

Wherever removing duplicates is involved, it's not a bad idea to use a set data structure.

JavaScript doesn't have a native set implementation, but the keys of an object work just as well - and in this case help because then the values can be used to keep track of how often an item appeared in the array:

function removeDuplicates(arr) {
    var counts = arr.reduce(function(counts, item) {
        counts[item] = (counts[item]||0)+1;
        return counts;
    }, {});
    return Object.keys(counts).reduce(function(arr, item) {
        if(counts[item] === 1) {
            arr.push(item);
        }
        return arr;
    }, []);
}

var myArr = [1, 1, 2, 5, 5, 7, 8, 9, 9];
removeDuplicates(myArr);

Check out the example on jsfiddle.

Alternately, you could not use calls to reduce(), and instead use for and for(item in counts) loops:

function removeDuplicates(arr) {
    var counts = {};
    for(var i=0; i<arr.length; i++) {
        var item = arr[i];
        counts[item] = (counts[item]||0)+1;
    }
    var arr = [];
    for(item in counts) {
        if(counts[item] === 1) {
            arr.push(item);
        }
    }
    return arr;
}

Check out the example on jsfiddle.

Actually, very few months ago i need to work with jquery array. i have multiple time duplicate value in javascript array. i don't require to display then again and again, i just want to remove that same value in jquery. i had found out the solution of remove duplicates value from array in jquery.

Here's my version

var a = [1, 1, 2, 5, 5, 7, 8, 9, 9];

function removeIfduplicate( arr ) {
    var discarded = [];
    var good      = [];
    var test;
    while( test = arr.pop() ) {
        if( arr.indexOf( test ) > -1 ) {
            discarded.push( test );
            continue;
        } else if( discarded.indexOf( test ) == -1 ) {
            good.push( test );
        }
    }
    return good.reverse();
}

x = removeIfduplicate( a );
console.log( x ); //[2, 7, 8]

Today, our topic is how to remove duplicates items from multidimensional array in jquery. i will help to remove duplicates from an array of objects in javascript. it's very simple to delete duplicate object from json array in jquery.

EDITED with better answer:

var myArr = [1, 1, 2, 5, 5, 7, 8, 9, 9];

function removeDuplicates(arr) {
    var i, tmp;
    for(i=0; i<arr.length; i++) {
        tmp = arr.lastIndexOf(arr[i]);
        if(tmp === i) {
            //Only one of this number
        } else {
            //More than one
            arr.splice(tmp, 1);
            arr.splice(i, 1);
        }
    }
}

You can use any one as you need. we can easily delete key value from array using splice function of js array. if you have jquery array object and you need to remove it then you can remove it using "delete". Let's see both example so it can help you for deleting key value from jquery array. so let's see that:

If it's just alphanumeric, duplicates are case-sensitive, and there can be no more than two of any element, then something like this can work:

var a = [2, 1, "a", 3, 2, "A", "b", 5, 6, 6, "B", "a"],

    clean_array = $.map(a.sort(), function (v,i) {
        a[i] === a[i+1] && (a[i] = a[i+1] = null);
        return a[i];
    });

// clean_array = [1,3,5,"A","B","b"]

How to count duplicate value in an array in javascript. 1020. November 30, 2017, at 10:16 AM. How do I override a JQuery (Datatable RowGroup) library function?

Remove Duplicate Elements from an Array using jQuery A very useful jQuery function is the $.unique () that removes all duplicate elements from an array of DOM elements. However this function only works on arrays of DOM elements, not strings or numbers. Thanks to an anonymous reader for pointing this out in the comments section.

For finding duplicate values in JavaScript array, you’ll make use of the traditional for loops and Array reduce method. Using For Loop Let’s see how you can find duplicates in an array using for loop. The logic is you’ll separate the array into two array, duplicate array and unique array. You’ll iterate over the values in the array and

A node is considered a duplicate if it is the exact same node as one already in the array; two different nodes with identical attributes are not considered to be duplicates. This function only works on plain JavaScript arrays of DOM elements, and is chiefly used internally by jQuery. You probably will never need to use it.

Comments
  • is it always ordered and/or numeric?
  • Could a value occur more than twice?
  • @jeschafe: why does that matter ? you can't do it in a sublinear time anyway ... if that's what you were after
  • @jeschafe It's filled with strings, not ordered. j08691: No, no value can occur more than twice.
  • Just because it can affect how the code is written. Some of the ones below don't work with strings in the aray.
  • can it possible in lodash
  • much as I like .reduce, I suspect a simple iteration over the counts object would be more efficient...
  • @Alnitak - Do you meant something like for item in counts { /*...*/ }?
  • Yes, that's what I meant. The overhead of calling a function for each iteration with .reduce can be quite high.
  • If you view the jspref in my answer you can see these don't perform that well
  • I was going to post an answer using this strategy. Here's is the jsFiddle I had written to help: jsfiddle.net/VBYun
  • At the end, myArr contains [1, 5, 7, 9] .
  • Ugh, I just realized this will only work if there are only 2 of the numbers in the array. If that's the max, there's a much easier way of checking the lastIndexOf() against the current i value. If they match, you're good, if they don't you can delete both based on the index you have for each.
  • var myArr = [1, 1, 2, 5, 5, 7, 8, 9, 9]; function removeDuplicates(arr) { var i, tmp; for(i=0; i<arr.length; i++) { tmp = arr.lastIndexOf(arr[i]); if(tmp === i) { //Only one of this number } else { //More than one arr.splice(tmp, 1); arr.splice(i, 1); } } } console.log(removeDuplicates(myArr)); ​