How to get an array unique Back

If an array does not have repeated items, it's a unique. Before discussing how to get an array unique in underscore, there have been various ways to complete it.

The most common way is to use another array to store items. If the items is not included in that array, then push it into the array, if not, then ignore it.

function unique(arr) {
    var res = [];
    var len = arr.length;

    for (var i = 0; i < len; i++) {
        var item = arr[i];

        var res_len = res.length;

        for (var j = 0; j < res_len; j++) {
            if (res[j] === item) {
                break;
            }
        }

        if (j === res_len) {
            res.push(item);
        }
    }

    return res;
}

If we don't consider the compatibility of browsers, we can use Array.prototype.indexOf() and Array.prototype.filter() to simplify it:

function unique(arr) {
    var res = arr.filter(function (item, index, array) {
        return array.indexOf(item) === index;
    });

    return res;
}

Sorting is another advanced way to improve your algorithms:

function unique(arr) {
    return arr.concat([]).sort().filter(function (item, index, array) {
        return !index || item !== array[index - 1];
    });
}

Of course, you can also use ES6 features of Array:

function unique(arr) {
    return Array.from(new Set(arr));
}

underscore.js has implemented _.unique() by considering whether the array is sorted by accepting a Boolean value isSorted as the second parameters, which is optional. If you have a look on it, you may find that it's familiar with the first implementation: using two loops to make an array unique.

However, there is a problem which is still can not be solved. If there is two objects which have the same key-value pairs, which is considered as same, the algorithm above still can not figure it out.

results matching ""

    No results matching ""