Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f002060

Browse files
committed
961_N-Repeated_Element_in_Size_2N_Array
1 parent 52f1d37 commit f002060

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ Also, there are open source implementations for basic data structs and algorithm
194194
| 946 | [Validate Stack Sequences](https://leetcode.com/problems/validate-stack-sequences/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/946_Validate_Stack_Sequences.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/946_Validate_Stack_Sequences.java) | Add a stack named inStack to help going through pushed and popped. O(n) and O(n) |
195195
| 953 | [Verifying an Alien Dictionary](https://leetcode.com/contest/weekly-contest-114/problems/verifying-an-alien-dictionary/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/953_Verifying_an_Alien_Dictionary.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/953_Verifying_an_Alien_Dictionary.java) | Use hashmap to store index of each value, then create a comparator based on this index, O(n) and O(n) |
196196
| 954 | [Array of Doubled Pairs](https://leetcode.com/contest/weekly-contest-114/problems/array-of-doubled-pairs/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/954_Array_of_Doubled_Pairs.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/954_Array_of_Doubled_Pairs.java) | Sort, then use hashmap to store the frequency of each value. Then, check n, 2 * n in hashmap, O(nlogn) and O(n) |
197+
| 961 | [N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/submissions/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/961_N-Repeated_Element_in_Size_2N_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/961_N-Repeated_Element_in_Size_2N_Array.java) | Hash and count number, O(n) and O(n) |
197198

198199
| # | To Understand |
199200
|---| ----- |
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[] A) {
3+
HashMap<Integer, Integer> hash = new HashMap<>();
4+
int ans = A[0];
5+
for (int n: A) {
6+
int count = hash.getOrDefault(n, 0) + 1;
7+
hash.put(n, count);
8+
if (count >= hash.get(ans)) ans = n;
9+
}
10+
return ans;
11+
}
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import collections
2+
3+
4+
class Solution(object):
5+
def repeatedNTimes(self, A):
6+
"""
7+
:type A: List[int]
8+
:rtype: int
9+
"""
10+
counter = collections.Counter(A)
11+
return counter.most_common(1)[0][0]
12+
13+
14+
if __name__ == '__main__':
15+
s = Solution()
16+
print s.repeatedNTimes([1, 2, 3, 3])
17+
print s.repeatedNTimes([2, 1, 2, 5, 3, 2])
18+
print s.repeatedNTimes([5, 1, 5, 2, 5, 3, 5, 4])

0 commit comments

Comments
 (0)