Skip to content

Commit 1957305

Browse files
chore: add LeetCode daily solution
1 parent 3426066 commit 1957305

5 files changed

Lines changed: 61 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# N-Repeated Element in Size 2N Array (Easy)
2+
3+
**Problem ID:** 961
4+
**Date:** 2026-01-02
5+
**Link:** https://leetcode.com/problems/n-repeated-element-in-size-2n-array/
6+
7+
## Approach
8+
9+
To solve the problem of finding the N-repeated element in a size 2N array, we can utilize a straightforward approach leveraging a set or a dictionary to track occurrences of elements.
10+
11+
### Approach:
12+
13+
1. **Understanding the Problem**: The array `nums` has a length of `2N` and contains `N + 1` unique elements, with exactly one element appearing `N` times. Our goal is to identify this repeated element.
14+
15+
2. **Data Structure**: We can use a dictionary (or a hash map) to count occurrences of each element as we iterate through the array. This allows us to efficiently check how many times each element appears.
16+
17+
3. **Iterating through the Array**: As we traverse the `nums` array:
18+
- For each element, we check if it already exists in our dictionary.
19+
- If it does, we increment its count.
20+
- If it doesn't, we add it to the dictionary with a count of 1.
21+
22+
4. **Finding the N-Repeated Element**: After populating the dictionary, we can simply iterate through its entries to find the element whose count equals `N`.
23+
24+
### Complexity:
25+
- **Time Complexity**: O(N), where N is the number of unique elements (which is at most 5000). This is because we make a single pass through the array to populate the dictionary and potentially another pass through the dictionary to find the repeated element.
26+
- **Space Complexity**: O(N) in the worst case, as we may store up to `N + 1` unique elements in our dictionary.
27+
28+
This approach is efficient and straightforward, leveraging the properties of hash maps for quick lookups and insertions, making it well-suited for this problem.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int repeatedNTimes(int[] nums) {
3+
Set<Integer> seen = new HashSet<>();
4+
for (int num : nums) {
5+
if (seen.contains(num)) {
6+
return num;
7+
}
8+
seen.add(num);
9+
}
10+
return -1; // This line will never be reached due to problem constraints
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var repeatedNTimes = function(nums) {
2+
const count = {};
3+
for (const num of nums) {
4+
if (count[num]) {
5+
return num;
6+
}
7+
count[num] = 1;
8+
}
9+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def repeatedNTimes(self, nums: List[int]) -> int:
3+
count = {}
4+
n = len(nums) // 2
5+
for num in nums:
6+
if num in count:
7+
count[num] += 1
8+
if count[num] == n:
9+
return num
10+
else:
11+
count[num] = 1

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
323323
- 2025-12-30 — [Magic Squares In Grid](https://leetcode.com/problems/magic-squares-in-grid/) (Medium) → `Medium/2025-12-30-840-Magic-Squares-In-Grid`
324324
- 2025-12-31 — [Last Day Where You Can Still Cross](https://leetcode.com/problems/last-day-where-you-can-still-cross/) (Hard) → `Hard/2025-12-31-1970-Last-Day-Where-You-Can-Still-Cross`
325325
- 2026-01-01 — [Plus One](https://leetcode.com/problems/plus-one/) (Easy) → `Easy/2026-01-01-66-Plus-One`
326+
- 2026-01-02 — [N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/) (Easy) → `Easy/2026-01-02-961-N-Repeated-Element-in-Size-2N-Array`

0 commit comments

Comments
 (0)