Thursday, July 3, 2014

javascript Function Remove Duplicates from JavaScript Array

function removeDuplicates(arr) {
    var temp = {};
    for (var i = 0; i < arr.length; i++)
        temp[arr[i]] = true;
 
    var r = [];
    for (var k in temp)
        r.push(k);
    return r;
}
 
//Usage
var fruits = ['apple', 'orange', 'peach', 'apple', 'strawberry', 'orange'];
var uniquefruits = removeDuplicates(fruits);
//print uniquefruits ['apple', 'orange', 'peach', 'strawberry'];

No comments:

Post a Comment