Skip to content

Commit 4c688d9

Browse files
authored
Merge pull request #1841 from arpy8/main
add 961-n-repeated-element-in-size-2n-array.md
2 parents 642e019 + 04a05bf commit 4c688d9

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
---
2+
id: n-repeated-element-in-size-2n-array
3+
title: N-Repeated Element in Size 2N Array
4+
sidebar_label: 961-N-Repeated-Element-in-Size-2N-Array
5+
tags:
6+
- Array
7+
- Hash Table
8+
- Counting
9+
description: "Given an array of size 2N, find the element that is repeated N times."
10+
---
11+
12+
## Problem
13+
14+
You are given an integer array `nums` with a length of `2n`, where `n` is an integer greater than 1. The array contains `n + 1` unique elements, and exactly one of these elements is repeated `n` times.
15+
16+
Return the element that is repeated `n` times.
17+
18+
### Examples
19+
20+
**Example 1:**
21+
22+
**Input:** `nums = [1,2,3,3]`
23+
**Output:** `3`
24+
25+
**Example 2:**
26+
27+
**Input:** `nums = [2,1,2,5,3,2]`
28+
**Output:** `2`
29+
30+
**Example 3:**
31+
32+
**Input:** `nums = [5,1,5,2,5,3,5,4]`
33+
**Output:** `5`
34+
35+
### Constraints
36+
37+
- `2 <= n <= 5000`
38+
- `nums.length == 2 * n`
39+
- `0 <= nums[i] <= 10^4`
40+
- `nums` contains `n + 1` unique elements and one of them is repeated exactly `n` times.
41+
42+
---
43+
44+
## Approach
45+
46+
To solve this problem, we can use a hash table (dictionary) to count the occurrences of each element. The element that appears `n` times is the one we need to return.
47+
48+
### Steps:
49+
50+
1. Initialize an empty dictionary to count the frequency of each element.
51+
2. Iterate through the array and update the count of each element in the dictionary.
52+
3. Check if the count of any element reaches `n` and return that element.
53+
54+
### Solution
55+
56+
#### Java
57+
58+
```java
59+
class Solution {
60+
public int repeatedNTimes(int[] nums) {
61+
Map<Integer, Integer> countMap = new HashMap<>();
62+
for (int num : nums) {
63+
countMap.put(num, countMap.getOrDefault(num, 0) + 1);
64+
if (countMap.get(num) == nums.length / 2) {
65+
return num;
66+
}
67+
}
68+
return -1; // Should never reach here
69+
}
70+
}
71+
```
72+
73+
#### C++
74+
75+
```cpp
76+
class Solution {
77+
public:
78+
int repeatedNTimes(vector<int>& nums) {
79+
unordered_map<int, int> countMap;
80+
int n = nums.size() / 2;
81+
for (int num : nums) {
82+
countMap[num]++;
83+
if (countMap[num] == n) {
84+
return num;
85+
}
86+
}
87+
return -1; // Should never reach here
88+
}
89+
};
90+
```
91+
92+
#### Python
93+
94+
```python
95+
class Solution:
96+
def repeatedNTimes(self, nums: List[int]) -> int:
97+
count = {}
98+
n = len(nums) // 2
99+
for num in nums:
100+
count[num] = count.get(num, 0) + 1
101+
if count[num] == n:
102+
return num
103+
return -1 # Should never reach here
104+
```
105+
106+
### Complexity Analysis
107+
108+
**Time Complexity:** O(n)
109+
>Reason: We iterate through the array once, and the operations we perform (insertion and lookup in a dictionary) are O(1) on average.
110+
111+
**Space Complexity:** O(n)
112+
>Reason: We use a dictionary to store the frequency of each element, which in the worst case can contain `n + 1` unique elements.
113+
114+
### References
115+
116+
**LeetCode Problem:** [N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/)

0 commit comments

Comments
 (0)