  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # Generate all permutation of an array without duplicate result  » cpp » Generate all permutation of an array without duplicate result

By : Igas
Date : November 17 2020, 11:55 AM
it fixes the issue This is a leetcode question permutation2. , With STL, the code may be: code :
``````std::vector<std::vector<int> > permuteUnique(std::vector<int> num) {
std::sort(num.begin(), num.end());
std::vector<std::vector<int> > res;
if(num.empty()) {
return res;
}
do {
res.push_back(num);
} while (std::next_permutation(num.begin(), num.end()));
return res;
}
``````
``````{2, 1, 1}
{1, 2, 1}
{1, 1, 2}
{1, 1, 2}
{1, 2, 1}
`````` ## C++ - is it possible to generate every single permutation of an array, once and only once, using random_shuffle?

By : user2714342
Date : March 29 2020, 07:55 AM
To fix the issue you can do Do you have to use random_shuffle? That seems like the wrong algorithm for this function, since there's no guarantee on how many attempts you need to actually get all your n! different permutations. A more suited algorithm would be std::next_permutation. As in:
code :
`````` do {
} while (std::next_permutation(v, v+5));
``````
``````for (int i = 0; i < 1000; ++i) {
std::random_shuffle(v, v+5);
}
`````` ## javascript skip duplicate permutation from array

By : Ian Alexander
Date : March 29 2020, 07:55 AM
this will help If you need to keep the digits only, then you can sort before storing:
code :
``````var newList = [],
list = [345, 453, 497, 479, 749, 128, 281, 812, 182, 218, 821, 128, 281, 812, 182, 218, 821, 497, 479, 749, 128, 281, 812, 182, 218, 821, 497, 128];
list.forEach(function(item) {
var norm = +String(item).split("").sort().join("");
if (newList.indexOf(norm) == -1) newList.push(norm); // or save item instead
})
console.log(newList);`````` ## Remove duplicate result from Permutation Function PHP if input contain same char

By : rarepool
Date : March 29 2020, 07:55 AM
should help you out You can pass your result to an array and then remove the duplicated ones and pass back to string.
First you use explode() on your string to create an array using the , as separator:
code :
``````\$array = explode(",", \$gege)
``````
``````\$array_unique = array_unique(\$array);
``````
``````\$unique_string = implode("," \$array_unique);
``````
``````\$gege = "ab,ba,ca,ca,ac,bc,cb,cb,ac,bc,cc,cc,";

echo implode(",",array_unique(explode(",", \$gege)));
``````
``````function stringUnique(\$string, \$separator){

return implode(\$separator,array_unique(explode(\$separator, \$string)));

}
``````
``````\$gege = "";
for(\$i = 0; \$i < getPermCount(\$letters, \$cek); \$i++) {
\$gege .=  getPerm(\$letters, \$cek, \$i).",";
}
echo stringUnique(\$gege, ",");
`````` ## Generate permutation matrix from permutation vector

By : A. Rothschild
Date : March 29 2020, 07:55 AM
around this issue I believe this can be done by reordering the rows of the identity matrix: ## How to generate an array of every permutation of a sequence, with duplicates?

By : sapna
Date : September 25 2020, 06:00 AM
like below fixes the issue One approach would be to use the length of the array as a base. You could then just access the array's elements from 0, and count up to the amount of combinations (array length ** length). If you're working with a small dataset, performance really shouldn't be an issue, but this answer is very performance oriented:
code :
``````const getCombos = (arr, len) => {
const base = arr.length
const counter = Array(len).fill(base === 1 ? arr : 0)
if (base === 1) return [counter]
const combos = []
const increment = i => {
if (counter[i] === base - 1) {
counter[i] = 0
increment(i - 1)
} else {
counter[i]++
}
}

for (let i = base ** len; i--;) {
const combo = []
for (let j = 0; j < counter.length; j++) {
combo.push(arr[counter[j]])
}
combos.push(combo)
increment(counter.length - 1)
}

return combos
}

const combos = getCombos([1, 2, 3, 4], 3)
console.log(combos)`````` 