
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Generate all combinations of supplied words in JavaScript
We are required to write a JavaScript function that takes in an array of strings. The function should then generate and return all possible combinations of the strings of the array.
Example
The code for this will be −
const arr = ['a', 'b', 'c', 'd']; const permutations = (len, val, existing) => { if(len==0){ res.push(val); return; } for(let i=0; i<arr.length; i++){ // so that we do not repeat the item, using an array here makes it O(1) operation if(!existing[i]){ existing[i] = true; permutations(len−1, val+arr[i], existing); existing[i] = false; } } } let res = []; const buildPermuations = (arr = []) => { for(let i=0; i < arr.length; i++){ permutations(arr.length−i, "", []); } }; buildPermuations(arr); console.log(res);
Example
And the output in the console will be −
[ 'abcd', 'abdc', 'acbd', 'acdb', 'adbc', 'adcb', 'bacd', 'badc', 'bcad', 'bcda', 'bdac', 'bdca', 'cabd', 'cadb', 'cbad', 'cbda', 'cdab', 'cdba', 'dabc', 'dacb', 'dbac', 'dbca', 'dcab', 'dcba', 'abc', 'abd', 'acb', 'acd', 'adb', 'adc', 'bac', 'bad', 'bca', 'bcd', 'bda', 'bdc', 'cab', 'cad', 'cba', 'cbd', 'cda', 'cdb', 'dab', 'dac', 'dba', 'dbc', 'dca', 'dcb', 'ab', 'ac', 'ad', 'ba', 'bc', 'bd', 'ca', 'cb', 'cd', 'da', 'db', 'dc', 'a', 'b', 'c', 'd' ]
- Related Articles
- How to generate all combinations of 3 or multiple columns in Excel?
- All combinations of sums for array in JavaScript
- Python - Generate all possible permutations of words in a Sentence
- Generate all combinations of a specific size from a single set in PHP
- How to generate a list of all possible 4 digits combinations in Excel?
- C++ Program to Generate All Possible Combinations of a Given List of Numbers
- Find all substrings combinations within arrays in JavaScript
- How to get all combinations of some arrays in JavaScript?
- C++ Program to Generate All Possible Combinations Out of a,b,c,d,e
- Finding all possible combinations from an array in JavaScript
- Algorithm to get the combinations of all items in array JavaScript
- JavaScript function that generates all possible combinations of a string
- Reverse all the words of sentence JavaScript
- Print all combinations of factors in C++
- Replace all occurrence of specific words in a sentence based on an array of words in JavaScript

Advertisements