Mark As Completed Discussion

Nested Loops: A Concern

The given code uses nested loops, resulting in a time complexity of O(n2), where n is the length of the array. In the worst case, this could lead to sluggish performance, particularly for larger datasets. In a real-world scenario, efficiency often matters a lot, so we'd prefer to optimize the code.

Step Three

A More Efficient Approach

Can we achieve the same result with just one loop, making it an O(n) solution? Yes, we can! Let's break down how we can do this.