Permutations/Combinations Algorithms Cheat Sheets
Tags: javascript, computerscience, algorithms, webdev
Published: 2018-06-29 12:11:52

---
title: Permutations/Combinations Algorithms Cheat Sheets
published: true
description: With explanations and links to examples in JavaScript
tags: javascript, computerscience, algorithms, webdev
---

<div id="ember4227" class="ember-view"><div class="reader-article-content"><h3>TL;DR</h3> 
<p>This article briefly describes the difference between mathematical <em>permutations</em> and <em>combinations,</em>explains the main idea behind permutations and combinations algorithms and contains links to algorithms implementation in JavaScript.</p> 
<blockquote>
  JavaScript code examples may be found in&nbsp; 
 <a href="https://github.com/trekhleb/javascript-algorithms" target="_blank" rel="nofollow noopener">JavaScript Algorithms and Data Structures</a>&nbsp;repository. Read more for further details. 
</blockquote> 
<h3>What are Combinations and Permutations</h3> 
<p>Let’s say we have a&nbsp;<strong>collection</strong>&nbsp;or&nbsp;<strong>set&nbsp;</strong>of something (collection of numbers, letters, fruits, coins etc.) and we need to&nbsp;<strong>pick items</strong>&nbsp;from a collection to form another collection. For example imagine that you’re picking lottery numbers from the collection of available numbers ({0, 1, 2, 3, 4, 5, 6, 7, 8, 9} → {4, 5, 9}). Or you’re picking the fruits from collections of available fruits to make a fruit salad ({orange, apple, banana, grape} → {apple, banana}). Or you’re trying to guess the lock password and you’re choosing 3 numbers out of {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} set to guess the correct password ({1, 1, 2}, {1, 1, 3}, {1, 1, 4},&nbsp;…). In all these cases you’re creating one collection out from the other one by following some rules. And these rules define whether your new collection is a&nbsp;<strong>permutation</strong>&nbsp;or a&nbsp;<strong>combination</strong>.</p> 
<p>If the&nbsp;<strong>order</strong>&nbsp;of the elements in your new collection&nbsp;<strong>matters</strong>&nbsp;then you’re dealing with&nbsp;<strong>permutations</strong>&nbsp;(for example in case of lock password the set of {1, 1, 2} is not the same as set of {2, 1, 1}). Otherwise if the&nbsp;<strong>order doesn’t matter&nbsp;</strong>then you’re making a&nbsp;<strong>combination&nbsp;</strong>(combination of fruits {apple, banana} to make a salad — it doesn’t matter if you’ll pick apple or banana first).</p> 
<p>Your new collection&nbsp;<strong>may or may not contain duplicates (or repetitions)</strong>. For example in lock password you’re allowed to use duplicate numbers but when you’re dealing with collection of race winners ({Black, Smith, Brown}) you’re not allowed to make duplicates because it doesn’t make sense to have the same person to be on two winning positions.</p> 
<p>To illustrate all these differences I’ve created small cheat sheets to make it more easy to remember them.</p> 
<h3>Permutations Concept Cheat&nbsp;Sheet</h3> 
<div class="slate-resizable-image-embed slate-image-embed__resize-full-width"> 
 <img class="progressiveMedia-image js-progressiveMedia-image" data-src="https://cdn-images-1.medium.com/max/2000/1*JNK-n0Pt0Vbxk0lxVpgT5A.png" src="https://cdn-images-1.medium.com/max/2000/1*JNK-n0Pt0Vbxk0lxVpgT5A.png"> 
</div> 
<h3>Combinations Concept Cheat&nbsp;Sheet</h3> 
<div class="slate-resizable-image-embed slate-image-embed__resize-full-width"> 
 <img class="progressiveMedia-image js-progressiveMedia-image" data-src="https://cdn-images-1.medium.com/max/2000/1*7cFRn8jW4g_91YgDAbmxRQ.png" src="https://cdn-images-1.medium.com/max/2000/1*7cFRn8jW4g_91YgDAbmxRQ.png"> 
</div> 
<h3>Permutations/Combinations Algorithms Cheat&nbsp;Sheet</h3> 
<p>If you would want to implement permutation and combination algorithms you might find the following permutation/combination algorithms cheat sheet useful.</p> 
<div class="slate-resizable-image-embed slate-image-embed__resize-bleed"> 
 <img class="progressiveMedia-image js-progressiveMedia-image" data-src="https://cdn-images-1.medium.com/max/2000/1*vLsSsZMnesCFPCYTYMbxrQ.png" src="https://cdn-images-1.medium.com/max/2000/1*vLsSsZMnesCFPCYTYMbxrQ.png"> 
</div> 
<h3>Combinations and Permutations in JavaScript</h3> 
<p>If you want to see exact implementations of these algorithms in JavaScript you may find follow these links:</p> 
<ul> 
 <li><a href="https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/combinations" target="_blank" rel="nofollow noopener">Combinations in JavaScript</a></li> 
 <li><a href="https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/permutations" target="_blank" rel="nofollow noopener">Permutations in JavaScript</a></li> 
</ul> 
<p>I hope you’ll find these cheat sheets helpful and they will help you to write combinations/permutations algorithms smoothly.</p> 
<p>Happy coding!</p></div>
</div>