August 2024
Intermediate to advanced
516 pages
11h 47m
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 an implementation of this algorithm. Let’s see if we can figure out its Big O efficiency:
| | function wordBuilder(array) { |
| | const collection = []; |
| | |
| | for (const [indexI, valueI] of array.entries()) { |
| | for (const [indexJ, valueJ] of array.entries()) { |
| | if (indexI !== indexJ) { |
| | collection.push(valueI + valueJ); |
| | } |
| | } |
| | } |
| | |
| | return collection; ... |
Read now
Unlock full access