Count distinct elements in array
<p>I have data that comes from my server to datatables.
I'm successfully populating my table but in footer callback I want to do some statistics.</p>

<p>Lets say I have data like so:</p>

<pre><code>var data = [{
    date: '2013-05-12',
    holiday: "One type of holiday",
    dayType: "Weekend"
}, {
    date: '2013-05-13',
    holiday: "Another type",
    dayType: "Weekend"
}, {
    date: '2013-05-14',
    holiday: "Another type",
    dayType: "Work"
}, {
    date: '2013-05-15',
    holiday: "",
    dayType: "Work"
}];
</code></pre>

<p>I would like to count number of days with different holidays.</p>

<p>Here is result I would like to get:</p>

<pre><code>var summary= [
{
    "One type of holiday": {
        "work": 0,
        "weekend": 1
    }
},
{
    "Another type": {
        "work": 1,
        "weekend": 1
    }
}];
</code></pre>

<p><a href="http://jsfiddle.net/Misiu/uhwbr/" rel="nofollow">I've created a very simple code to simply aggregate holidays</a>:</p>

<pre><code>for (var i = 0; i &lt; data.length; i++) {
    //console.log(data[i].holiday);
    /*other stuff here*/
    if (data[i].holiday.length &gt; 0) 
        summary[data[i].holiday] = summary[data[i].holiday] + 1 || 1;
}
</code></pre>

<p>but this gives me invalid results, because in my data array holiday contains spaces.</p>

<p>I need a way to fix this and to split holidays based on dayType.</p>

<hr>

<p><strong>MY SOLUTION:</strong>
<a href="http://jsfiddle.net/Misiu/NgNMs/2/" rel="nofollow">My version of answer</a>:</p>

<pre><code>var summary = {}, d, tmp, type;
for (var i = 0; i &lt; data.length; i++) {
    var d = data[i];
    if (d.holiday.length &gt; 0) {
        type = d.dayType == 'Weekend' || d.dayType == 'Free' ? 'Weekend' : 'Work';
        tmp = summary[d.holiday];
        if (!tmp) {
            tmp = {
                Weekend: 0,
                Work: 0
            };
            summary[d.holiday] = tmp;
        }
        summary[d.holiday][type] += 1;
    }
}
</code></pre>

<p>Because this is modified version of @Arun answer I'm not posting this as standalone answer.<br/>
I find my version easier to understand, hope someone find's it useful.</p>
