4

that is found here in stack but i want somes changes.

    function perms(data) {
    if (!(data instanceof Array)) {
        throw new TypeError("input data must be an Array");
    }

    data = data.slice();  // make a copy
    var permutations = [],
        stack = [];

    function doPerm() {
        if (data.length == 0) {
            permutations.push(stack.slice());
        }
        for (var i = 0; i < data.length; i++) {
            var x = data.splice(i,1);
            stack.push(x);
            doPerm();
            stack.pop();
            data.splice(i, 0, x);
        }
    }

    doPerm();
    return permutations;
}

var input = "552".split('');
var result = perms(input);
for (var i = 0; i < result.length; i++) {
    result[i] = result[i].join('-');
}

The result of that is :

5-5-2

5-2-5

5-5-2

5-2-5

2-5-5

2-5-5

but , are 3 elements duplicates the result must be :

5-5-2

5-2-5

2-5-5

how can i fix that issue .

2 Answers2

3

Basically, you have one issue,

var x = data.splice(i, 1)[0];
//                       ^^^ is missing

because you get an array with splicing. The result is a deep nested array with

data.splice(i, 0, x);

This inserts the array later on position i.

For preventing duplicates, you need a check, if the actual value is already inserted in the result set with

permutations.some(function (a) {
    return a.every(function (b, j) {
        return stack[j] === b;
    });
}) || permutations.push(stack.slice());

which test the arrays and if no match, the push is performed.

function perms(data) {
    if (!(data instanceof Array)) {
        throw new TypeError("input data must be an Array");
    }

    data = data.slice(); // make a copy
    var permutations = [],
        stack = [],
        hash = Object.create(null);

    function doPerm() {
        if (data.length == 0) {
            permutations.some(function (a) {
                return a.every(function (b, j) {
                    return stack[j] === b;
                });
            }) || permutations.push(stack.slice());
            return;
        }
        for (var i = 0; i < data.length; i++) {
            var x = data.splice(i, 1)[0];
            stack.push(x);
            doPerm();
            stack.pop();
            data.splice(i, 0, x);
        }
    }

    doPerm();
    return permutations;
}

var input = "552".split('');
var result = perms(input);
for (var i = 0; i < result.length; i++) {
    result[i] = result[i].join('-');
}

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Check if array array is present within resulting array before calling .push() using Array.prototype.some(), Array.prototype.join()

function p(a, b, res) {
  var b = b || [],
    res = res || [],
    len = a.length;
  if (!len) {
    // check if `res` contains `b.join("")`  
    if (!res.length 
       || !res.some(function(n) {
            return n.join("") === b.join("")
          }))
      res.push(b)
  } else {
    for (var i = 0
        ; i < len; p(a.slice(0, i).concat(a.slice(i + 1, len))
          , b.concat(a[i]), res)
          , i++);
  }
  return res
}

var result = p("552".split(""));

result = result.map(function(res) {
  return res.join("-")
});

console.log(result);
guest271314
  • 1
  • 15
  • 104
  • 177
  • Utilizing `.some()`, `.join()` at `if` condition within `perms` function at OP https://jsfiddle.net/xqLm7f6o/. See also [Permutations without recursive function call](http://stackoverflow.com/questions/34013675/permutations-without-recursive-function-call) – guest271314 Mar 11 '17 at 20:45