0

I am trying to compare dates stored in the format 'Y-M-D H-i-s' from one array in the hopes of removing the duplicates and creating a count along side thr orignal date, I'm comparing the dates using the following code:

function compare(a, b){
            if(a.getDate() == b.getDate() && a.getMonth() == b.getMonth() && a.getFullYear() == b.getFullYear()){
                return true;
            }else{
                return false;
            };
        };

This is how I am looping through them:

    times.forEach(function(timeOne){
        times.forEach(function(timeTwo){
            if(compare(timeOne, timeTwo)){
                console.log("same");
            }else{
                console.log("different");
                count.push(timeOne);
            };
        });
    });

When I do this it does not seem to work and simply removes the first 1619 values, it does not push to the count array and causes my browser to crash. Any suggestions on how to overcome this or a better way to achieve what I need. I am also unsure at the moment on how to create the count along side.

Edit ---

Here is the remaining code for the programme:

var results = <?php echo $results; ?>,                                                                                                                 
            times = [],                                                                                                                                        
            count = [];
     results.forEach(function(result){                                                                                                                      
         times.push(new Date(result.time));                                                                                                                 
     });

I would also like to mention that the items array is near on 30,000 entries. So I need a way that will allow to to cut down on processing time drastically.

hudsond7
  • 666
  • 8
  • 25
  • This might help you, there are a ton of ways to check for duplicates and make a new unique array: http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array, on top of that can you show where you initialize count? – Royalty Jan 09 '15 at 17:50
  • @Royalty Edit added, will check link. – hudsond7 Jan 09 '15 at 17:51

2 Answers2

1

I'll give some tips. Maybe they'll solve your problem.

First, you can reduce your code:

function compare(a, b){
    if(a.getDate() == b.getDate() && a.getMonth() == b.getMonth() && a.getFullYear() == b.getFullYear()){
        return true;
    }else{
        return false;
    };
};

to

function compare(a, b){
    return a.getDate() == b.getDate() && a.getMonth() == b.getMonth() && a.getFullYear() == b.getFullYear();
};

Second, your loop is wrong. The inner loop is looping the i variable instead of j:

for(var j = 0; j < times.length-1; i++){
        ...
};

Third, since you're trying to remove duplicates, your should skip elements which i == j, because they'll always be equal. So add:

if(i == j) continue;

To the inner loop.

And Fourth, your approach is wrong. You're pushing to the count array if an element is not the same to other element. This does not garantee no duplicates. See, if you have and array with [1, 2, 2, 3, 4], and try to remove the duplicates using your algorithm, the result array will be like this [1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]. This is because you're searching dupes by element, but you should search it by array. You algorithm must garantee that there's only one of the kind in your array. A proper loop would be:

for(var i = 0; i < times.length; i++){
    if(times[i] == null || times[i] == undefined) continue;
    if(!contains(count, times[i])){
        count.push(times[i]);
    }
}

function contains(arr, elm){
    for(var i = 0; i < arr.length; i++){
        if(compare(elm, arr[i]))
            return true;
    }
    return false;
}

The count array now should have only one kind of each date, with no dupes.

AFTER EDIT:

Wow. 30000 entries. With 30000 entries the approach must be another. Try this solution, see if it solves for you, but I believe that it is not for your case.

Dalton
  • 420
  • 4
  • 12
  • Very informative, worked and well. On a 30,000+ array took milliseconds. Thank you. – hudsond7 Jan 09 '15 at 18:03
  • I have a question for you, how would I go about making this multi dimensional? for example adding a frequency with each date? I have tried to alter the code given to no luck. – hudsond7 Jan 09 '15 at 19:01
  • Depends on how you create your data structure. – Dalton Jan 09 '15 at 19:38
0
for(var i = 0; i < times.length-1; i++){
    for(var j = 0; j < times.length-1; i++){
        if((i!=j) && times[i] && times[j]){
            if(compare(times[i], times[j]) == true){
                console.log("same!!!");
            }else{
                console.log("not same!");
                count.push(times[i]);
            };
        };
    };
};
imnancysun
  • 612
  • 8
  • 14