
- 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
Find all substrings combinations within arrays in JavaScript
We are required to write a JavaScript function that takes in an array of strings. The function should find all the substring and superstring combinations that exist in the array and return an array of those elements.
For example − If the array is −
const arr = ["abc", "abcd", "abcde", "xyz"];
Then the output should be −
const output = ["abc", "abcd", "abcde"];
because the first two are the substring of last.
Example
The code for this will be −
const arr = ["abc", "abcd", "abcde", "xyz"]; const findStringCombinations = (arr = []) => { let i, j, res = {}; for (i = 0; i < arr.length - 1; i++) { if (res[arr[i]]) { continue; }; for (j = i + 1; j < arr.length; j++) { if (res[arr[j]]) { continue; } if (arr[i].indexOf(arr[j]) !== -1 || arr[j].indexOf(arr[i]) !== -1) { res[arr[i]] = true; res[arr[j]] = true; } }; }; const result = arr.filter(el => res[el]); return result; }; console.log(findStringCombinations(arr));
Output
And the output in the console will be −
[ 'abc', 'abcd', 'abcde' ]
- Related Articles
- How to get all combinations of some arrays in JavaScript?
- Finding the sum of all common elements within arrays using JavaScript
- Adding paragraph tag to substrings within a string in JavaScript
- Generating combinations from n arrays with m elements in JavaScript
- All combinations of sums for array in JavaScript
- Generate all combinations of supplied words in JavaScript
- Get Equal Substrings Within Budget in C++
- Finding all possible combinations from an array in JavaScript
- Get all substrings of a string in JavaScript recursively
- Find substrings that contain all vowels in Python
- Find all substrings in a string using C#
- C# Program to find all substrings in a string
- Algorithm to get the combinations of all items in array JavaScript
- JavaScript function that generates all possible combinations of a string
- Find all possible substrings after deleting k characters in Python

Advertisements