Find All Duplicates in an Array — Java Coding Problem
Difficulty: easy | Category: array
Problem Description
Given an integer array `nums` of length `n` where all integers are in the range `[1, n]`, some elements appear **twice** and others appear **once**. Return an array of all elements that appear **twice**, sorted in ascending order. **Constraints:** - `n == nums.length` - `1 <= n <= 10⁵` - `1 <= nums[i] <= n` - Each element appears once or twice.
Examples
Example 1
Input: nums = [4,3,2,7,8,2,3,1]
Output: [2,3]
Example 2
Input: nums = [1,1,2]
Output: [1]
Example 3
Input: nums = [1,2]
Output: []