Sobes.tech
Middle

There are two arrays: A1 and A2, where A2 = A1 (reference type). What needs to be done only with A2 to clear both arrays?

sobes.tech AI

Answer from AI

If A2 is a reference to the same array as A1 (i.e., A2 = A1), then clearing one of them will clear both, as they point to the same object.

To clear both arrays, it is enough to call the clear method on one of them, for example:

A2.length = 0; // clears the array referenced by both A1 and A2

Or

A2.splice(0, A2.length);

Important: you cannot simply assign A2 = [] — this will create a new array and will not affect A1.