permutations

import { permutations } from "jsr:@ende/collections";

function

function permutations<T>(inputArray: Iterable<T>): T[][]

Builds all possible orders of all elements in the given array. Ignores equality of elements, meaning this will always return the same number of permutations for a given length of input.

inputArray: Iterable<T>

The array to build permutations from.

Returns

An array of all possible permutations of the given array.

Example: Basic usage

import { permutations } from "@ende/collections/permutations";
import { assertEquals } from "@ende/assert";

const numbers = [ 1, 2 ];
const windows = permutations(numbers);

assertEquals(windows, [
  [ 1, 2 ],
  [ 2, 1 ],
]);

Source