Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing 2D and 3D array in JavaScript

I am initializing my 2D array using the below snippet:

let arr = new Array(m).fill().map(() => new Array(n).fill(-1));

There are some other ways too to initialize it what I need help is in language like C++ there is a simpler way to initialize it using memset. How is time complexity and space complexity affected compared to C++ or Java when using this snippet?

like image 694
Rashidtvmr Avatar asked Feb 13 '26 14:02

Rashidtvmr


1 Answers

In c++, with memset the time complexity is O(log n). however that if you're going to do any future modifications to the object, It would cost O(n).

like image 76
jbjaveed Avatar answered Feb 16 '26 04:02

jbjaveed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!