# Surely there has to be a better way to do this

**URL:** https://discourse.processing.org/t/surely-there-has-to-be-a-better-way-to-do-this/17246
**Category:** Coding Questions
**Created:** [January 22, 2020, 5:43am UTC](https://discourse.processing.org/t/surely-there-has-to-be-a-better-way-to-do-this/17246 "2020-01-22T05:43:57Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ZachMcMkay](https://avatars.discourse-cdn.com/v4/letter/z/8e7dd6/32.png) [@ZachMcMkay](https://discourse.processing.org/u/ZachMcMkay)
#### Post date: [January 22, 2020, 5:43am UTC](https://discourse.processing.org/t/surely-there-has-to-be-a-better-way-to-do-this/17246/1 "2020-01-22T05:43:57Z")

</div>

What i wanted to do was take an array of integers and get all permutations of that array keeping the same length. I was using the array [1,2,3,4,5] after trying for a bit i just decided to do it the hard way. However im sure there is a better way. If you’ve ever done this before I’d love to see how you did it. This is how i did it in an ugly non scalable way.

```auto
let nums = [1,2,3,4,5];
let perms = [];

function permutate(arr){

	for(let i = 0; i < arr.length; i++){
		for(let j = 0; j < arr.length; j++){
			for(let k = 0; k < arr.length; k++){
				for(let l = 0; l < arr.length; l++){
					for(let m = 0; m < arr.length; m++){
						if(i != j && i != k && i != l && i != m && j != k && j != l && j != m && k != l && k != m && l != m){
							let newArr = [arr[i],arr[j],arr[k],arr[l],arr[m]];
							perms.push(newArr);
						}
					}
				}
			}
		}
	}
}

permutate(nums);

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [January 22, 2020, 6:08am UTC](https://discourse.processing.org/t/surely-there-has-to-be-a-better-way-to-do-this/17246/2 "2020-01-22T06:08:03Z")

</div>

found some info here:

> **[Permutations of an Array in Java | Baeldung](https://www.baeldung.com/java-array-permutations)**
>
> A quick and practical guide to generating array permutations in Java.

---

<div class="post-metadata">

### Author: ![bzSteve](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bzsteve/32/7358_2.png) [@bzSteve](https://discourse.processing.org/u/bzSteve)
#### Post date: [January 22, 2020, 5:59pm UTC](https://discourse.processing.org/t/surely-there-has-to-be-a-better-way-to-do-this/17246/3 "2020-01-22T17:59:17Z")

</div>

Here’s an iterative version of Heap’s algorithm. It’s able to handle large arrays of any data type without stack overflows, it does not alter the original list order, and it calls a user-specified function for each iteration.

```auto
const perm = ( list, func ) => {
	const len = list.length
	const indices = []
	const listCopy = list.slice( )
	
	for ( let i = 0; i < len; i++ ) indices.push( 0 )
	
	let idx = 1
	
	func && func( list )
	
	while ( idx < len ) {
		if ( indices[idx] < idx ) {
			const swap = idx % 2 * indices[idx]
			
			;[listCopy[ swap], listCopy[idx] ] = [listCopy[ idx], listCopy[swap] ]
			
			func && func( listCopy )
			
			indices[idx]++
			idx = 1
		} else {
			indices[idx++] = 0
		}
	}
}

const nums = [1, 2, 3, 4, 5]

perm( nums, ( list ) => console.log( list.join( ', ' ) ) )

```
