August 2020
Intermediate to advanced
508 pages
11h 53m
English
The next example is an algorithm that collects every combination of two-character strings built from an array of single characters. For example, given the array: ["a", "b", "c", "d"], we’d return a new array containing the following string combinations:
| | [ |
| | 'ab', 'ac', 'ad', 'ba', 'bc', 'bd', |
| | 'ca', 'cb', 'cd', 'da', 'db', 'dc' |
| | ] |
Following is a JavaScript implementation of this algorithm. Let’s see if we can figure out its Big O efficiency:
| | function wordBuilder(array) { |
| | let collection = []; |
| | |
| | for(let i = 0; i < array.length; i++) { |
| | for(let j = 0; j < array.length; j++) { |
| | if (i !== j) { |
| | collection.push(array[i] + array[j]); |
| | } |
| | } |
| | } |
| | |
| | return collection; |
| | } |
Here we’re running ...
Read now
Unlock full access