Skip to content

Commit 7a3839e

Browse files
authored
Create 0961. N-Repeated Element in Size 2N Array
1 parent eeb3d23 commit 7a3839e

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/****************************************
2+
3+
961. N-Repeated Element in Size 2N Array
4+
5+
In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times.
6+
Return the element repeated N times.
7+
8+
Example 1:
9+
Input: [1,2,3,3]
10+
Output: 3
11+
12+
Example 2:
13+
Input: [2,1,2,5,3,2]
14+
Output: 2
15+
16+
Example 3:
17+
Input: [5,1,5,2,5,3,5,4]
18+
Output: 5
19+
20+
Note:
21+
4 <= A.length <= 10000
22+
0 <= A[i] < 10000
23+
A.length is even
24+
25+
****************************************/
26+
27+
28+
29+
/******************** Solution01 ********************/
30+
class Solution {
31+
public int repeatedNTimes(int[] A) {
32+
Map<Integer, Integer> map = new HashMap<>();
33+
for(int x : A){
34+
Integer count = map.get(x);
35+
if(count == null) {
36+
map.put(x, 1);
37+
}
38+
else {
39+
return x;
40+
}
41+
}
42+
return 0;
43+
}
44+
}
45+
46+
47+
/******************** Solution02 ********************/
48+
class Solution {
49+
public int repeatedNTimes(int[] A) {
50+
for (int i = 0; i < A.length; i ++) {
51+
for (int j = i+1; j < A.length; j ++) {
52+
if (A[i] == A[j]) {
53+
return A[i];
54+
}
55+
else {continue;}
56+
}
57+
}
58+
return 0;
59+
}
60+
}

0 commit comments

Comments
 (0)