|
| 1 | +--- |
| 2 | +id: find-all-duplicates-in-an-array |
| 3 | +title: find-all-duplicates-in-an-array |
| 4 | +sidebar_label: 0442 find-all-duplicates-in-an-array |
| 5 | +tags: |
| 6 | + - hashmap |
| 7 | + - LeetCode |
| 8 | + - Java |
| 9 | + - Python |
| 10 | + - C++ |
| 11 | +description: This is a solution to the. Find All Duplicates in an Array problem on LeetCode |
| 12 | +--- |
| 13 | + |
| 14 | +## Problem Description |
| 15 | + |
| 16 | +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. |
| 17 | + |
| 18 | +You must write an algorithm that runs in $O(n)$ time and uses only constant extra space. |
| 19 | + |
| 20 | +### Examples |
| 21 | + |
| 22 | +**Example 1:** |
| 23 | + |
| 24 | +``` |
| 25 | +
|
| 26 | +Input: nums = [4,3,2,7,8,2,3,1] |
| 27 | +Output: [2,3] |
| 28 | +
|
| 29 | +``` |
| 30 | + |
| 31 | +**Example 2:** |
| 32 | + |
| 33 | + |
| 34 | +``` |
| 35 | +Input: nums = [1,1,2] |
| 36 | +Output: [1] |
| 37 | +``` |
| 38 | + |
| 39 | +**Example 3:** |
| 40 | + |
| 41 | + |
| 42 | +``` |
| 43 | +Input: nums = [1] |
| 44 | +Output: [] |
| 45 | +``` |
| 46 | + |
| 47 | + |
| 48 | +### Constraints |
| 49 | + |
| 50 | +- $1 \leq \text{nums.length} \leq 105$ |
| 51 | +- Each element in nums appears once or twice. |
| 52 | + |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +## Solution for Find All Duplicates in an Array |
| 57 | + |
| 58 | +### Intuition |
| 59 | + |
| 60 | + - Since each element in the array represents a valid index `(1 to n)`, we can leverage this information. |
| 61 | + - We might be able to sort the array and then compare adjacent elements to find duplicates efficiently. |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +### Approach |
| 66 | + |
| 67 | + - Utilize existing order: We know that each element should appear at most twice and all values are in the range `[1, n]`. This suggests some inherent order in the array. |
| 68 | + - Leverage sorting: By sorting the array, we can group duplicate elements together, making them easier to identify. |
| 69 | + - Iterate and compare: After sorting, iterating through the array, we can compare adjacent elements. If two consecutive elements are identical, that element is a duplicate. |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +#### Code in Different Languages |
| 76 | + |
| 77 | +<Tabs> |
| 78 | + <TabItem value="Python" label="Python"> |
| 79 | + <SolutionAuthor name="@parikhitkurmi"/> |
| 80 | + |
| 81 | + ```python |
| 82 | +//python |
| 83 | + |
| 84 | +class Solution: |
| 85 | + def findDuplicates(self, nums: List[int]) -> List[int]: |
| 86 | + ans =[] |
| 87 | + n=len(nums) |
| 88 | + for x in nums: |
| 89 | + x = abs(x) |
| 90 | + if nums[x-1]<0: |
| 91 | + ans.append(x) |
| 92 | + nums[x-1] *= -1 |
| 93 | + return ans |
| 94 | + |
| 95 | +``` |
| 96 | + </TabItem> |
| 97 | + <TabItem value="Java" label="Java"> |
| 98 | + <SolutionAuthor name="@parikhitkurmi"/> |
| 99 | + |
| 100 | + ```java |
| 101 | +//java |
| 102 | + |
| 103 | + |
| 104 | +class Solution { |
| 105 | + public List<Integer> findDuplicates(int[] nums) { |
| 106 | + List<Integer> ans = new ArrayList<>(); |
| 107 | + int n = nums.length; |
| 108 | + for (int i = 0; i < n; i++) { |
| 109 | + int x = Math.abs(nums[i]); |
| 110 | + if (nums[x - 1] < 0) { |
| 111 | + ans.add(x); |
| 112 | + } |
| 113 | + nums[x - 1] *= -1; |
| 114 | + } |
| 115 | + return ans; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +``` |
| 121 | +</TabItem> |
| 122 | +<TabItem value="C++" label="C++"> |
| 123 | +<SolutionAuthor name="@parikhitkurmi"/> |
| 124 | + |
| 125 | + ```cpp |
| 126 | +//cpp |
| 127 | + |
| 128 | +class Solution { |
| 129 | +public: |
| 130 | + vector<int> findDuplicates(vector<int>& nums) { |
| 131 | + vector<int>ans; |
| 132 | + int n=size(nums); |
| 133 | + for(int i=0;i<n;i++){ |
| 134 | + int x=abs(nums[i]); |
| 135 | + if(nums[x-1]<0){ |
| 136 | + |
| 137 | + ans.push_back(x); |
| 138 | + } |
| 139 | + nums[x-1]*=-1; |
| 140 | + } |
| 141 | + return ans; |
| 142 | + } |
| 143 | +}; |
| 144 | + |
| 145 | +``` |
| 146 | +
|
| 147 | + </TabItem> |
| 148 | +</Tabs> |
| 149 | +
|
| 150 | +
|
| 151 | +
|
| 152 | +
|
| 153 | +
|
| 154 | +## References |
| 155 | +
|
| 156 | +- **LeetCode Problem:** [Continuous Subarray Sum](https://leetcode.com/problems/find-all-duplicates-in-an-array/) |
| 157 | +- **Solution Link:** [Continuous Subarray Sum](https://leetcode.com/problems/find-all-duplicates-in-an-array/submissions/) |
| 158 | +- **Authors GeeksforGeeks Profile:** [parikhit kurmi](https://www.geeksforgeeks.org/user/sololeveler673/) |
| 159 | +- **Authors Leetcode:** [parikhit kurmi](https://leetcode.com/u/parikhitkurmi14/) |
0 commit comments