Find All Duplicates in an Array

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

Find all the elements that appear twice in this array.

Could you do it without extra space and in O(n) runtime?

Example:
Input:
[4,3,2,7,8,2,3,1]

Output:
[2,3]

Solution:

class Solution {
    public List<Integer> findDuplicates(int[] nums) {
        // [4,3,2,7,8,2,3,1]
        //  7 3 2 4 8 2 3 1
        //  3 3 2 4 8 2 7 1
        //  2 3 3 4 8 2 7 1
        //  3 2 3 4 8 2 7 1
        //  3 2 3 4 1 2 7 8
        Set<Integer> result = new HashSet();
        for (int i = 0; i < nums.length; i ++) {
            int currVal = nums[i];
            int currValTargetIndex = currVal - 1;
            int currIndexTargetVal = i + 1;
            while (currVal != currIndexTargetVal) {
                if (nums[currValTargetIndex] == currVal) {
                    result.add(currVal);
                    break;
                } else {
                    swap(nums, i, currValTargetIndex);
                }
                currVal = nums[i];
                currValTargetIndex = currVal - 1;
            }
        }
        return new ArrayList(result);
    }
    
    private void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}