Given an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears once or twice, return an array of all the integers that appears twice.

You must write an algorithm that runs in&nbsp;O(n)&nbsp;time and uses only constant extra space.

&nbsp;
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]
Output: []

&nbsp;
Constraints:


	n == nums.length
	1 &lt;= n &lt;= 105
	1 &lt;= nums[i] &lt;= n
	Each element in nums appears once or twice.


class Solution:
    def findDuplicates(self, nums: List[int]) -> List[int]:
        res = []
        hm = {}
        # adding entries in hashmap to check frequency
        for i, v in enumerate(nums):
            if v not in hm:
                hm[v] = 1
            else:
                hm[v] += 1
        # checking frequency of item and adding output to an array
        for key, value in hm.items():
            if value > 1:
                res.append(key)
        return res
class Solution {
    public List<Integer> findDuplicates(int[] nums) {
        List<Integer> ans = new ArrayList<>();
        for(int i=0;i<nums.length;i++){
            int ind = Math.abs(nums[i])-1;
            if(nums[ind]<0){
                ans.add(Math.abs(nums[i]));
            }
            else{
                nums[ind] = -1*nums[ind];
            }
        }
        return ans;
    }
}
class Solution {
public:
    vector<int> findDuplicates(vector<int>& nums) {
        int n=nums.size();
        vector<int> a;
        vector<int> arr(n+1,0);
        for(int i=0;i<nums.size();i++)
            arr[nums[i]]++;

        for(int j=0;j<=n;j++)
            if(arr[j]==2) a.push_back(j);

        return a;
    }
};
// Approach : Mark Visited Elements in the Input Array itself
var findDuplicates = function(nums) {
    let result = [];
    for(let i = 0; i < nums.length; i++) {
        let id = Math.abs(nums[i]) - 1;
        if(nums[id] < 0) result.push(id + 1);
        else nums[id] = - Math.abs(nums[id]);
    }
    return result;
};
Find All Duplicates in an Array