array-unique2

by shan10213223

JavaScript

var _ = {};

// _.uniq(array)
// Produces a duplicate-free version of the array, using === to test equality.
// In particular only the first occurence of each value is kept.
_.uniq = function (array) {
  let output = [];
  for (let item of array) {
    let i = output.indexOf(item);
    if (i === -1) {
      output.push(item);
    }
  }
  return output;
};

test1 = _.uniq([1, 'a', 3, 1]);
test2 = _.uniq(['1', '2', '3', '4', '3']);
test3 = _.uniq([1,2,1,3,4,3]);
console.log(test1, test2, test3);