-1
var MyArray = [
    [1, "07/28/2014"],
    [2, "07/29/2014"],
    [3, "07/28/2014"],
    [4, "07/30/2014"],
    [5, "07/28/2014"],
];

In this array, how to remove the duplicate columns and find count the removed items.

I need individual count for removed items.

like

07/28/2014 is 2 times.

This is my Code:

function(MyArray, search_val)
{
    var counter = 0; alert(MyArray.length);

    for(var i = 0; i < MyArray.length; i++) 
    {
        if(MyArray[i][0] == search_val)
        {   
             MyArray.splice(i, 1);
             counter++;
        }
    }
    alert("counter: "+counter);
    return counter;
}
Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
ABC
  • 41
  • 1
  • 5

2 Answers2

1

Here are a couple very naive basic solutions. Both of these examples are for demonstration purposes only. Do not use in production code. There are parameter validation and other checks that were left out to simplify the examples. Further, depending on your browser requirements, or available frameworks, there are much faster and better ways to do your equality tests. Think of this as a starting point for more research.

I'll leave it as an exercise to improve these answers.

For your data exclusively, this would work:

var count, isFirst, data = [[1,"07/28/2014"], [2,"07/29/2014"],[3, "07/28/2014"],[1,"07/28/2014"],[4, "07/30/2014"]];

count = 0
/* loop over each element of the array */
for(var x = 0; x < data.length; x++) {
    isFirst = true // used to skip the first matching element
    /* for each loop iteration, loop over every element in the array */
    for(var y = 0; y < data.length; y++) {
        /*
           check the inner loop element against the outer loop element
           to see if they satisfy your equality requirements.
           Notice the second set of index operator brackets, this is
           how you access the next dimension of the array.
        */
        if(data[x][1] === data[y][1]) {
            /*
               If this is not the first time we've found this match
               then this must be a duplicate element, so remove it
            */
            if (!isFirst) {
                data.splice(y, 1);
                count++;
            }

            isFirst = false // makes sure that future matches are removed
        }
    }
}

console.log(count);
console.log(data);

For a more general solution one possibility would be to pass in the equality test as an anonymous function:

/* Note, this is the same algorithm as above */
function spliceIf(data, predicate) {
    var count = 0, isFirst;
    for(var x = 0; x < data.length; x++) {
        isFirst = true;
        for(var y = 0; y < data.length; y++) {
            if (predicate(data[x], data[y])) {
                if (!isFirst) {
                    data.splice(y, 1);
                    count++;
                }

                isFirst = false
            }
        }
    }
    return count;
}

var items = [[1,"07/28/2014"], [2,"07/29/2014"],[3, "07/28/2014"],[1,"07/28/2014"],[4, "07/30/2014"]];
// Now call the new function and pass in the equality test as the second parameter:
var itemsRemoved = spliceIf(items,
    function(a, b) {
        /*
            This predicate function will be passed to spliceIf. When it
            is called from within then spliceIf function, it will be 
            provided with the inner and outer elements of the loop.
            You can then do your equality test however you see fit.

            Notice the predicate function must return a value.

            This is equivalent to the "data[x][1] === data[y][1]" line
            in the example above.
        */
        return a[1] === b[1];
    }
);

console.log(itemsRemoved);
console.log(items);
klyd
  • 3,939
  • 3
  • 24
  • 34
0

Just doing some fiddles, I thought this might help. JSFiddle of Duplicate Remover Function

var data = [
    [1, "07/28/2014"],
    [2, "07/29/2014"],
    [3, "07/28/2014"],
    [4, "07/30/2014"],
    [5, "07/28/2014"],
];
var data2 = [
    [1, "07/28/2014"],
    [2, "07/29/2014"],
    [3, "07/29/2014"],
    [4, "07/29/2014"],
    [5, "07/29/2014"],
];

    function removeDuplicates(Array){    
        this._newArray = [];
        this.numberOfDuplicates = 0;
        this.listDuplicates = [];
        //Remove Duplicates    
            for(i=0;i<Array.length;i++){      
                for(j=0;j<Array.length;j++){
                    if(!Array[i][1]) //skip the current loop if index is empty
                        break;
                    if(Array[i][1]==Array[j][1] && i!=j){
                        this.listDuplicates.push(Array[j]);                            
                        Array[j]=0;
                        this.numberOfDuplicates+=1; //increase counter for dupes
                    }
                }    
            }
        //Recreate Array

        this.newArray = function(){
            for(i=0;i<Array.length;i++){
                if(Array[i])
                    this._newArray.push(Array[i]);
            }
        return this._newArray;
        }
}

var data = new removeDuplicates(data);
console.log(data.newArray());
console.log(data.numberOfDuplicates + " Duplicates");
console.log(data.listDuplicates);

console.log("\n");
var data2 = new removeDuplicates(data2);
console.log(data2.newArray());
console.log(data2.numberOfDuplicates + " Duplicates");
console.log(data2.listDuplicates);
Mike Ante
  • 746
  • 1
  • 6
  • 18
  • ThanQ Mike, It should be fine. But small issue is there, i need to check individually date wise counting.. Here total duplicate counts are showing. I need to find duplicates datewise counting.. Can you edit this code please.. – ABC Aug 12 '14 at 05:34
  • Ok Fine. Thank you so much. I have some doubt. Plz give me clarification. If i have 2 rows in same dates, now i deleted 1 duplicate row. counter was not updated to 1. Still showing counter is 2. what's the problem? – ABC Aug 12 '14 at 06:07
  • This might be my last update. Don't forget to mark this as an answer. http://jsfiddle.net/gfg2926d/4/ – Mike Ante Aug 12 '14 at 06:57