0% found this document useful (0 votes)
0 views

JavaScript interview questions

Dense arrays contain items at every index, while sparse arrays have at least one missing item at any index. The document provides examples of both types of arrays and explains how to flatten multi-dimensional arrays using the spread operator, recursive functions, and the Array flat method. It demonstrates different levels of flattening for multi-dimensional arrays.

Uploaded by

anooshaasehar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

JavaScript interview questions

Dense arrays contain items at every index, while sparse arrays have at least one missing item at any index. The document provides examples of both types of arrays and explains how to flatten multi-dimensional arrays using the spread operator, recursive functions, and the Array flat method. It demonstrates different levels of flattening for multi-dimensional arrays.

Uploaded by

anooshaasehar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

What is the difference between dense and sparse arrays?

An array contains items at each index starting from first(0) to last(array.length - 1) is called as
Dense array. Whereas if at least one item is missing at any index, the array is called as sparse.

Let's see the below two kind of arrays,

const avengers = ["Ironman", "Hulk", "CaptainAmerica"];

console.log(avengers[0]); // 'Ironman'

console.log(avengers[1]); // 'Hulk'

console.log(avengers[2]); // 'CaptainAmerica'

console.log(avengers.length); // 3

const justiceLeague = ["Superman", "Aquaman", , "Batman"];

console.log(justiceLeague[0]); // 'Superman'

console.log(justiceLeague[1]); // 'Aquaman'

console.log(justiceLeague[2]); // undefined

console.log(justiceLeague[3]); // 'Batman'

console.log(justiceLeague.length); // 4

How do you flattening multi dimensional arrays

Flattening bi-dimensional arrays is trivial with Spread operator.

const biDimensionalArr = [11, [22, 33], [44, 55], [66, 77], 88, 99];

const flattenArr = [].concat(...biDimensionalArr); // [11, 22, 33, 44, 55, 66, 77, 88, 99]

But you can make it work with multi-dimensional arrays by recursive calls,

function flattenMultiArray(arr) {

const flattened = [].concat(...arr);

return flattened.some((item) => Array.isArray(item))


? flattenMultiArray(flattened)

: flattened;

const multiDimensionalArr = [11, [22, 33], [44, [55, 66, [77, [88]], 99]]];

const flatArr = flattenMultiArray(multiDimensionalArr); // [11, 22, 33, 44, 55, 66, 77, 88, 99]

Also you can use the flat method of Array.

const arr = [1, [2, 3], 4, 5, [6, 7]];

const fllattenArr = arr.flat(); // [1, 2, 3, 4, 5, 6, 7]

// And for multiDimensional arrays

const multiDimensionalArr = [11, [22, 33], [44, [55, 66, [77, [88]], 99]]];

const oneStepFlat = multiDimensionalArr.flat(1); // [11, 22, 33, 44, [55, 66, [77, [88]], 99]]

const towStep = multiDimensionalArr.flat(2); // [11, 22, 33, 44, 55, 66, [77, [88]], 99]

const fullyFlatArray = multiDimensionalArr.flat(Infinity); // [11, 22, 33, 44, 55, 66, 77, 88, 99]

⬆ Back to Top

You might also like