//全排列
var arr = ["a", "b", "c", "d"];
//实现全排列
function AllPermuAndCombi(arr,length){
var Allresult=[];
for(var i=1;i<=length;i++){
Allresult=Allresult.concat(mainFunction(arr,i));
}
return Allresult;
}
function mainFunction(Arr, num) {
var resultArr = [],
resultNum = 0,
iNow = 1;
if(num == 1) {
return Arr;
}
function PermuAndCombina(arr, iNow, str) {
for(var i = 0; i < arr.length; i++) {
var result = arr.concat();
var strClone = str;
strClone += result.splice(i, 1);
if(iNow == num) {
resultNum += result.length;
for(var j = 0; j < result.length; j++) {
resultArr.push(strClone + result[j]);
}
} else {
PermuAndCombina(result,iNow+1, strClone);
}
}
}
//组合,涉及的是数组去重
function unique(arr){
for (var i=0;i<arr.length;i++) {
arr[i]=arr[i].split("").sort().join("");
}
var result=[];
var obj={};
for (var i = 0; i < arrength; i++) {
if (!obj[arr[i]]) {
result.push(arr[i]);
obj[arr[i]]=1;
}
}
return result;
}
PermuAndCombina(Arr,iNow+1, "");
// resultArr.push(resultNum);
//resultArr=unique(resultArr)
return resultArr;
}
console.log(mainFunction(arr,2));
console.log(AllPermuAndCombi(arr,3));
//组合
var Arr_un=[1,5,3,1,7,3];
//数组去重
function Array_unique(Arr){
var result=[];
var Obj={};
for (var i=0;i<Arr.length;i++) {
if (!Obj[Arr[i]]) {
result.push(Arr[i]);
Obj[Arr[i]]=1;
}
}
return result;
}
console.log(Array_unique(Arr_un));