Jeetu Alex - IT Software Professional specialized in developing high-end, customized business solutions using Coldfusion and Java.

Coldfusion
Java
JQuery

An online resource for latest web technologies like Coldfusion, JRun, Pro*C, JQuery, HTML5, PHP, W3C, Java, J2EE, C, C++, ORACLE, PL/SQL, MySql, Ajax, Coldbox, Fusebox, UNIX, JavaScript, NodeJS and much more...

Friday, March 10, 2017

How to get all possible combinations of elements in an array including order and lengths

Below is a recursive function which gives all possible unique combinations of elements in a array in all order and lengths.

Alternate 1

function getAllCombinations(arr) {
  var inputArray = arr;
  var resultArray = [];
  var combine = function() {
    var args = arguments;
    var temp = [];
    for (var i in args) {
      temp.push(inputArray[args[i]]);
    }
    if (temp.length > 0) {
      resultArray.push(temp);
    }
    for (i in inputArray) {
      temp = [];
      for (var j in args) {
        if (args[j] == i) {
          temp = false;
          break;
        } else {
          temp.push(args[j]);
        }
      }
      if (temp) {
        temp.push(i);
        combine.apply(null, temp);
      }
    }
    return resultArray;
  };
  return combine();
}
Alternate 2

function getAllCombinations(arr) {
  var inputArray = arr;
  var resultArray = [];
  var conditionNext = true;
  var combine = function() {
    var args = arguments;
    var temp = [];
    for (var i in args) {
      temp.push(inputArray[args[i]]);
    }
    if (temp.length > 0) {
      resultArray.push(temp);
    }
    for (i in inputArray) {
      conditionNext = true;
      if (args.length !== 0) {
        for (var j in args) {
          if (args[j] == i) {
            conditionNext = false;
          }
        }
      }
      if (conditionNext) {
        temp = [];
        for (j in args) {
          temp.push(args[j]);
        }
        temp.push(i);
        if (temp.length > 0) {
          combine.apply(null, temp);
        }
      }
    }
    return resultArray
  };
  return combine();
}

Alternate 3

function getAllCombinations(arr) {
  var inputArray = arr;
  var resultArray = [];

  var checkCondition = function(args, nextIndex) {
    if (args.length === 0) {
      return true;
    }
    for (var i = 0; i < args.length; i++) {
      if (args[i] == nextIndex) {
        return false;
      }
    }
    return true;
  };

  var applyFunc = function(args, index) {
    var temp = [];
    for (var i = 0; i < args.length; i++) {
      temp.push(args[i]);
    }
    temp.push(index);

    if (temp.length > 0) {
      v.apply(null, temp);
    }
  };

  var populateResult = function(args) {
    var temp = [];
    for (var i = 0; i < args.length; i++) {
      temp.push(inputArray[args[i]]);
    }
    if (temp.length > 0) {
      resultArray.push(temp);
    }
  };

  var v = function() {
    var args = arguments;
    populateResult(args);
    for (var i = 0; i < inputArray.length; i++) {
      if (checkCondition(args, i)) {
        applyFunc(args, i);
      }
    }
  };
  v();
  return resultArray;
}
See the complete solution discussion here

0 comments :

Post a Comment