LeetCode Entry
961. N-Repeated Element in Size 2N Array
Duplicate majority element
961. N-Repeated Element in Size 2N Array easy blog post substack youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1223
Problem TLDR
Duplicate majority element #easy
Intuition
The easy intuition is just to count frequencies.
Approach
- frequency of 2 is enough
- if first element skipped then majority voting solution applicable
- another fun solution is to look at sum and uniq’s sum
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\)
Code
// 35ms
fun repeatedNTimes(n: IntArray) =
(n.sum() - n.toSet().sum())/(n.size/2-1)
// 0ms
pub fn repeated_n_times(n: Vec<i32>) -> i32 {
let (mut j, mut f) = (0, 0);
n[(1..n.len()).find(|&i| {
if f == 0 { j = i }
f += (n[0]==n[i]||n[i]==n[j]) as i32 * 2 -1; f > 1
}).unwrap_or(j)]
}