DEV Community

Ajay Kumar Verma
Ajay Kumar Verma

Posted on • Originally published at weekendtutorial.com

How to generate all subarrays in javascript in 2022?

How to generate all subarrays in javascript in 2022?

Hello Readers,

Interviewers like to ask questions about the subarrays as it gives a clear indication of the logical thinking and aptitude of a candidate.

Considering the above, we are going to start a new tag MCAIQ (most commonly asked interview questions) on weekendtutorial that will contain the most frequently asked questions in the interview. This will be your single stop for all such questions and interview preparations.

we will see how to generate all the subarrays of an array in javascript using iterative and recursive ways both.


Generate all subarrays using iteration

Problem Statement

Given an array, generate all the possible subarrays of the given array using an iterative way.

Input : [1, 2, 3]

Output: [1], [1, 2], [1, 2, 3], [2], [2, 3], [3]

Input: [1, 2]

Output: [1], [1, 2], [2]
Enter fullscreen mode Exit fullscreen mode

Implementation


Generate all subarrays using recursion

Implementation


I hope you have learned something if you like the article please share it with friends and help weekendtutorial to grow. This site is run by ads and it is required for it to be alive and grow.

Read the following articles –

  1. Learn Nginx in-depth
  2. Implement binary tree using javascript

This is all for today. Please visit our other articles, and keep learning.

Top comments (0)