HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptModerate

Filter out duplicates from an array and return only unique value

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
uniquearrayreturnvaluefilteronlyandfromoutduplicates

Problem

I have the following script, which filters duplicates and returns only unique values for an array.
When passing false or nothing the "original" array is overwritten, when passing true a new array is created with unique values and returned instead.

The script should work only with numbers and strings.

I would like to know:

  • If it is possible to optimize the code for maximum performance and how,



  • If it is possible to improve or write in alternative way logic for createArray, maybe a better way in term of code repetition.



https://jsfiddle.net/dj7qxyL7/

Array.prototype.getUnique = function (createArray) {
        createArray = createArray === true ? true : false;
        var temp = JSON.stringify(this);
        temp = JSON.parse(temp);
        if (createArray) {
            var unique = temp.filter(function (elem, pos) {
                return temp.indexOf(elem) == pos;
            }.bind(this));
            return unique;
        }
        else {
            var unique = this.filter(function (elem, pos) {
                return this.indexOf(elem) == pos;
            }.bind(this));
            this.length = 0;
            this.splice(0, 0, unique);
        }
    }

    var duplicates = [0, 0, 1, 1, 2, 3, 1, 1, 0, 4, 4];
    console.log('++++ ovveride')
    duplicates.getUnique();
    console.log(duplicates);
    console.log('++++ new array')
    var duplicates2 = [0, 0, 1, 1, 2, 3, 1, 1, 0, 4, 4];
    var unique = duplicates2.getUnique(true);
    console.log(unique);
    console.log('++++ original')
    console.log(duplicates2);

Solution

If is possible to improve or write in alternative way logic for
createArray, maybe a better way in term of code repetition.

Why give the option to mutate the array? Most array methods return a new array, including filter, so it seems redundant in your code, since you get a new array anyway. Not mutating the array is usually the right thing to do. In any case, using JSON to clone an array is very inefficient, when all you need is var copy = array.slice(); and a recursive solution for nested arrays. If you get rid of this unusual requirement, and create your own function instead of extending the prototype, then you are left with what unique really is:

var unique = function(xs) {
  return xs.filter(function(x, i) {
    return xs.indexOf(x) === i
  })
}



If it is possible to optimize the code for maximum performance and how

If you only deal with things that can be stringified properly, like numbers, and obviously strings, then you can improve performance by using a simple object to keep track of items that have been seen already:

var unique = function(xs) {
  var seen = {}
  return xs.filter(function(x) {
    if (seen[x])
      return
    seen[x] = true
    return x
  })
}


Although this would consider 1 and '1' to be a duplicate.

Code Snippets

var unique = function(xs) {
  return xs.filter(function(x, i) {
    return xs.indexOf(x) === i
  })
}
var unique = function(xs) {
  var seen = {}
  return xs.filter(function(x) {
    if (seen[x])
      return
    seen[x] = true
    return x
  })
}

Context

StackExchange Code Review Q#83717, answer score: 10

Revisions (0)

No revisions yet.