Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

# No repeats please and Heap's algorithm

See original GitHub issue

#### Issue Description

I’m going nuts. Could someone please walk me through Heap’s algorithm? Why do we check `int` to see if it’s odd or even and why do we determine which elements to swap based on that? Where does this odd or even idea come from? Assuming that I don’t know this algorithm, how can I solve this challenge?

`````` function perm(str) {

// Split the string into an array of characters.
var arr = str.split('');
var permutations = [];
var tmp;

// Function to swap variables' content.
function swap(index1, index2) {
tmp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = tmp;
}

//Generate arrays of permutations using the algorithm.
function generate(int) {      // int is length of array
if (int === 1) {
// Make sure to join the characters as we create  the permutation arrays
permutations.push(arr.join(''));
} else {
for (var i = 0; i != int; ++i) {
generate(int - 1);
if (int % 2 === 0) {
swap(i, int - 1);
} else {
swap(0, int - 1);
}
}
}
}

generate(arr.length);

return permutations;
}

console.log(perm("abcd"));

``````

### Issue Analytics

• State:
• Created 7 years ago

1reaction
dhcodescommented, Nov 7, 2016

@fhdhsni maybe try again today?

0reactions
texas2010commented, Nov 7, 2016

@Mongues looks like you may have accidentally subscribed to all notifications for the FreeCodeCamp repository. To stop receiving notifications please visit the subscriptions page and select “Not watching”. Additionally, you can visit the GitHub Help page on managing notification delivery methods for more help.

We are sorry for your inconvenience!

#### Top Results From Across the Web

Free Code Camp (FCC) - No Repeats Please (Heap's algorithm)
A good start today will be solving Free Code Camp's No Repeats Please problem throug the use of Heap's Algorithm.
No repeats please, Heap's algorithm and frustration with ...
“No repeats please” is a hard challenge. Solve it whatever way you can first, then come back and refactor. Give yourself a break...
No repeats please and Heap's algorithm · Issue #11553 - GitHub
I'm going nuts. Could someone please walk me through Heap's algorithm? Why do we check int to see if it's odd or even...
Free Code Camp: No Repeats Please - K. Anthony
To do this, most folks went to Heap's algorithm, which is an algorithm for generating all possible permutations of a number of objects....
Heap's Algorithm for generating permutations - GeeksforGeeks
Heap's algorithm is used to generate all permutations of n objects. The idea is to generate each permutation from the previous permutation ...

#### Troubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free