loop inside loop over one array I have the following code:


const arr = Array.from({length: 5}, (_, i) =&gt; i + 1);
const finalArray = [];
for (let i = 0; i &lt; arr.length; i++) {
   for (let j = 0; j &lt; arr.length; j++) {
       finalArray.push(arr[i] + '-' + arr[j])
   }
}

console.log(finalArray);



Which creates an array of range 1 =&gt; 5, and returns an array with a combination of all elements in the original array.
This code runs in O(n²), is it possible to make it more efficient?
No
You cannot output \$\Theta(n^2)\$ items in \$o(n^2)\$ time. Output size is a trivial lower bound of any algorithms.