Enumerating Arrays in JavaScript 
Tags: javascript, node, permutation, combination
Published: 2018-01-14 06:41:40

---
title: Enumerating Arrays in JavaScript 
published: true
description: enumerating elements in an array
tags: JavaScript, node,  permutation,combination
---
Enumeration is a process of arranging or selecting elements from an array in a certain way. They are also called permutation and combination. Let's look at several ways of enumerating an array in JavaScript

## Enumerating all Arrangement

Suppose there are __k__ positions to be filled and __n__ elements available in an array to fill these positions. Each position can take any values from __n__ elements. Then, there are  __n<sup>k</sup>__ different ways of filling the positions. Let's look at ways of evaluating result of this kind of enumeration.

```js
  var enumAll = require('enum-all')

  var arr = ['a','b','c','1','2','3']

  //Generate all three letter password using characters from arr

  var output = enumAll(arr, 3)
  console.log(output) //['aaa', 'baa', 'caa' .... 213 more items]
  
```

## Enumerating Power Set

Power set is a set containing all possible sub-set of elements in an array. That is, given an array containing __n__ elements its powerset contains all possible combinations of  elements of various sizes.The size of powerset is __2<sup>n</sup>__ and let's look at ways of evaluating powerset of elements in an array.

```js
  var powerSet = require('enum-powerset')

  var arr = ['Alice', 'Bob', 'Eve']

  //generate powerset of above elements
  var output = powerSet(arr)

  // Prints the powerset of three elements in arr
  console.log(output) 

  // [ [''],
  //   ['Alice'],
  //   ['Bob'],
  //   ['Alice,Bob'],
  //   ['Eve'],
  //   ['Alice,Eve]',
  //   ['Bob,Eve'],
  //   ['Alice,Bob,Eve' ]
  // ]
```

## Enumerating certain combination

Power set contains all possible combinations of elements of various sizes. Suppose you just want combinations of size __k__ from __n__ elements then, this kind of enumeration comes in handy. There are total __nc<sub>k</sub>__ ways of choosing k elements from n elements which equals n!/(k!*(n-k)!) where __!__ refers to factorial. Let's look at an example of this.

```js
  var nck = require('enum-nck')

  var teams =['united', 'city', 'liverpool']

  //generate all possible football matches between above teams
  var output = nck(teams, 2)

  //prints the result
  console.log(output)
  //[ [ 'united', 'city' ],
  //  [ 'united', 'liverpool' ],
  //  [ 'city', 'liverpool' ], 
  //]
```

## Enumerating permutation

Permutation is a way of evaluating all different ways __n__ elements of an array can be arranged. There are __n!(factorial)__ ways of permutating elements in array if each element is unique. Let's look at way of evaluating permutation of elements.

```js
  var permutate = require('enum-permutate')

  var arr = ['0','1','2']

  //generate permutation of above elements
  var output = permutate(arr)

  //prints output
  console.log(output)
  //[ [ '0', '1', '2' ],
  //  [ '0', '2', '1' ],
  //  [ '1', '0', '2' ],
  //  [ '1', '2', '0' ],
  //  [ '2', '0', '1' ],
  //  [ '2', '1', '0' ] 
  //]
```

All the above example works in node.js as well as browser (through bundler such as browserify).