Nodejs Array Unique unique()

Here you can find the source of unique()

Method Source Code

/*//from   w w w .  j a  va  2s.co m

Bonfire: Mutations

Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.

For example, ["hello", "Hello"], should return true because all of the letters in the second string are present in the first, ignoring case.

The arguments ["hello", "hey"] should return false because the string "hello" does not contain a "y".

Lastly, ["Alien", "line"], should return true because all of the letters in "line" are present in "Alien".

*/

Array.prototype.unique = function() {
    return this.reduce(function(accum, current) {
        if (accum.indexOf(current) < 0) {
            accum.push(current);
        }
        return accum;
    }, []);
};
function mutation(arr) {
  var a1 = arr[0].toLowerCase();
  var a2 = arr[1].toLowerCase();
  a1 = a1.split('');
  a2 = a2.split('');
  a1 = a1.sort();
  a2 = a2.sort();
  a1 = a1.unique();
  a2 = a2.unique();
  for (var i =0; i<a2.length;i++)
    {
      if (a1.indexOf(a2[i]) === -1)
        return false;
    }
  return true;
}

mutation(['Hello', 'HhelL']);

Related

  1. unique()
    Array.prototype.unique = function() {
      var output = {};
      for (var key = 0; key < this.length; key++) {
        output[this[key]] = this[key];
      var results = [];
      for (key in output) {
        var value = output[key];
        results.push(value);
    ...
    
  2. unique()
    Array.prototype.unique = function(){
        var temp = {};
        for (var i = 0; i < this.length; i++)
            temp[this[i]] = true;
        var r = [];
        for (var k in temp)
            r.push(k);
        return r;
    
  3. unique()
    Array.prototype.unique = function () {
      var tmp = {}, result = [];
      for (var i = 0; i < this.length; i++) {
        if (!tmp[this[i]]) {
          result.push(this[i]);
          tmp[this[i]] = true;
      return result;
    ...
    
  4. unique()
    var fs = require('fs');
    Array.prototype.unique = function() {
      var res = [],
        hash = {};
      for (var i = 0, elem;
        (elem = this[i]) != null; i++) {
        if (!hash[elem]) {
          res.push(elem);
          hash[elem] = true;
    ...
    
  5. unique()
    Array.prototype.unique = function() {
        var newArr = [],
            origLen = this.length,
            found, x, y;
        for (x = 0; x < origLen; x++) {
            found = undefined;
            for (y = 0; y < newArr.length; y++) {
                if (this[x] === newArr[y]) {
                    found = true;
    ...
    
  6. unique()
    Array.prototype.unique = function() {
        var a = this.concat();
        for(var i=0; i<a.length; ++i) {
            for(var j=i+1; j<a.length; ++j) {
                if(a[i] === a[j])
                    a.splice(j--, 1);
        return a;
    ...
    
  7. unique()
    Array.prototype.unique = function()
        this.sort();
        var re=[this[0]];
        for(var i = 1; i < this.length; i++)
            if( this[i] !== re[re.length-1])
                re.push(this[i]);
    ...
    
  8. unique()
    Array.prototype.unique = function() {
      var n = {},r=[];
      for(var i = 0; i < this.length; i++) {
        if ( this[i] && !n[this[i]]) {
          n[this[i]] = true;
          r.push(this[i]);
      return r;
    ...
    
  9. unique()
    Array.prototype.unique = function () {
      var obj,
        result = [];
      for (var i = 0, l = this.length; i < l; ++i) {
        obj = this[ i ];
        if ( result.indexOf( obj ) < 0 ) {
          result[ result.length ] = obj;
      return result;
    };