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

<p>You must write an algorithm that runs in <code>O(n)</code> time and uses only <em>constant</em> auxiliary space, excluding the space needed to store the output</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [4,3,2,7,8,2,3,1]
<strong>Output:</strong> [2,3]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [1,1,2]
<strong>Output:</strong> [1]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> nums = [1]
<strong>Output:</strong> []
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == nums.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= n</code></li>
	<li>Each element in <code>nums</code> appears <strong>once</strong> or <strong>twice</strong>.</li>
</ul>

class Solution:
    def findDuplicates(self, nums: List[int]) -> List[int]:
        # for i in range(len(nums)):
        #     while nums[i] != nums[nums[i] - 1]:
        #         nums[nums[i] - 1], nums[i] = nums[i], nums[nums[i] - 1]
        
        # return [num for i, num in enumerate(nums) if num != i + 1]

        # ------------------------------------------------------------
        n = len(nums)
        count = 0
        for i in range(n):
            if nums[abs(nums[i]) - 1] > 0:
                nums[abs(nums[i]) - 1] = -nums[abs(nums[i]) - 1]
            else:
                nums.append(abs(nums[i]))
                count += 1
        for i in range(count):
            nums[i] = nums.pop()
        for i in range(n - count):
            nums.pop()
        return nums
        # ------------------------------------------------------------

        # easy peasy python solution O (n) by marking
        result = []
        for num in nums:
            num = abs(num)
            if nums[num - 1] < 0:
                result.append(num)
            else:
                nums[num - 1] = -nums[num - 1]
        return result

        # ------------------------------------------------------------

        # Time - O(n)
        # Space - O(n)
        dic = {}
        result = []

        for n in nums:
            if n not in dic:
                dic[n] = 1
            else:
                result.append(n)

        return result
22400