Skip to content

Instantly share code, notes, and snippets.

@ggondim
Created April 23, 2021 00:36
Show Gist options
  • Save ggondim/895fde0ffc50221fb0fa5eeecf22a59b to your computer and use it in GitHub Desktop.
Save ggondim/895fde0ffc50221fb0fa5eeecf22a59b to your computer and use it in GitHub Desktop.
Array Combination (n, m=2)- Combinação simples de n elementos com m = 2 - Checagem com fatorial
const fatorial = (n) => n === 0 ? 1 : n * fatorial(n - 1);
const combinacoes = (n, m) => fatorial(n) / (fatorial(m) * fatorial(n - m));
function combinacoesDoisElementos(array: any[]) {
return array.reduce((combinacoes, item, i) => array
.slice(i+1)
.map(c => [item, c])
.concat(combinacoes), []);
}
function assertCombinacao(array) {
return assertCombinacao(array).length === combinacoes(array.length, 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment