Solution: Find Duplicates in Array
Let's look at the various solutions for finding the duplicates in an array.
Solution #1: Brute force
This solution iterates over the input array for each given element, starting from the element after the given element, and checks if another element is equal to it or not (lines 6-8). If it is equal, then it is added to the output list —duplicates (line 9). However, when you have more than 2 copies of any given element, the element is added to the ArrayList as many times as it is present. This does not look great, as shown in the case of the array on line 25 (11 occurs thrice in the output). We can check if the element has already been counted, as in solution # 2.
Time complexity
The array is iterated once for each element present so the time complexity is in ...