In the following code, parameter A represents the array to be de-duplicated.

1. ES6 Minimalist edition

[... new Set(a)]
Copy the code

2. Unordered array deduplicating (no sorting step)

function uniquify(a) {
    var i = 1;
    while(i < a.length) {
        a.slice(0, i).indexOf(a[i]) >= 0 ? a.splice(i, 1) : i++;
    }
    return a;
}
Copy the code

3. Ordered array decrement (array sorted)

function uniquify(a) {
    var i = 1;
    a.sort(function(a, b) { return a - b; });
    while(i < a.length) {
        a[i] === a[i - 1]? a.splice(i,1) : i++;
    }
    return a;
}
Copy the code

4. Ordered array deduplication (the array is sorted to reduce the number of delete operations, thus optimizing the time complexity)

function uniquify(a) {
    a.sort(function(a, b) { return a - b; });
    var i = 0, j = 0;
    while(++j < a.length) {
        if(a[i] ! == a[j]) { a[++i] = a[j]; } } a.length =Math.min(i + 1, a.length);
    return a;
}
Copy the code