Skip to content

Commit 1ca198d

Browse files
add 961
1 parent f6f9870 commit 1ca198d

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Your ideas/fixes/algorithms are more than welcome!
2929

3030
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
3131
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
32+
|961|[N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_961.java) | O(n) | O(1) | |Easy|
3233
|944|[Delete Columns to Make Sorted](https://leetcode.com/problems/delete-columns-to-make-sorted/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_944.java) | O(n) | O(1) | |Easy|
3334
|941|[Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_941.java) | O(n) | O(1) | |Easy|
3435
|933|[Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_933.java) | O(n) | O(n) | |Easy|
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* 961. N-Repeated Element in Size 2N Array
8+
*
9+
* In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times.
10+
*
11+
* Return the element repeated N times.
12+
*
13+
* Example 1:
14+
*
15+
* Input: [1,2,3,3]
16+
* Output: 3
17+
* Example 2:
18+
*
19+
* Input: [2,1,2,5,3,2]
20+
* Output: 2
21+
* Example 3:
22+
*
23+
* Input: [5,1,5,2,5,3,5,4]
24+
* Output: 5
25+
*
26+
*
27+
* Note:
28+
*
29+
* 4 <= A.length <= 10000
30+
* 0 <= A[i] < 10000
31+
* A.length is even
32+
* */
33+
public class _961 {
34+
public static class Solution1 {
35+
public int repeatedNTimes(int[] A) {
36+
Set<Integer> set = new HashSet<>();
37+
for (int num : A) {
38+
if (!set.add(num)) {
39+
return num;
40+
}
41+
}
42+
return -1;
43+
}
44+
}
45+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._961;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _961Test {
10+
private static _961.Solution1 solution1;
11+
private static int[] A;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _961.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
A = new int[]{1, 2, 3, 3};
21+
assertEquals(3, solution1.repeatedNTimes(A));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
A = new int[]{2, 1, 2, 5, 3, 2};
27+
assertEquals(2, solution1.repeatedNTimes(A));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
A = new int[]{5, 1, 5, 2, 5, 3, 5, 4};
33+
assertEquals(5, solution1.repeatedNTimes(A));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)