leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit 9e00d6582e7b3758610b8dab014180dc15171c00
parent 090091712aea5abfb0b7dfc17db3a00e6fc131b5
Author: Jack Sangdahl <jack@pngu.org>
Date:   Thu,  1 Jan 2026 19:45:06 -0700

961. n-repeated element in size 2n array

Diffstat:
An_repeat_elem_size_2n_arr.c | 28++++++++++++++++++++++++++++
An_repeat_elem_size_2n_arr.py | 24++++++++++++++++++++++++
2 files changed, 52 insertions(+), 0 deletions(-)

diff --git a/n_repeat_elem_size_2n_arr.c b/n_repeat_elem_size_2n_arr.c @@ -0,0 +1,28 @@ +// 961. N-Repeated Element in Size 2N Array +#include "leetcode.h" + +/* + * you are given an integer array 'nums' with the following properties: + * 'nums.length == 2 * n', 'nums' contains 'n + 1' unique elements, and exactly + * one element of 'nums' is repeated 'n' times. return the element that is + * repeated 'n' times. + */ + +int repeatedNTimes(int *nums, int numsSize) { + bool hash[10001] = {false}; + for (int i = 0; i < numsSize; ++i) { + if (hash[nums[i]]) + return nums[i]; + else + hash[nums[i]] = true; + } + return -1; +} + +int main() { + int n1[] = {1, 2, 3, 3}, n2[] = {2, 1, 2, 5, 3, 2}, + n3[] = {5, 1, 5, 2, 5, 3, 5, 4}; + printf("%d\n", repeatedNTimes(n1, ARRAY_SIZE(n1))); // expect: 3 + printf("%d\n", repeatedNTimes(n2, ARRAY_SIZE(n2))); // expect: 2 + printf("%d\n", repeatedNTimes(n3, ARRAY_SIZE(n3))); // expect: 5 +} diff --git a/n_repeat_elem_size_2n_arr.py b/n_repeat_elem_size_2n_arr.py @@ -0,0 +1,24 @@ +# 961. N-Repeated Element in Size 2N Array + +""" +you are given an integer array 'nums' with the following properties: +'nums.length == 2 n', 'nums' contains 'n + 1' unique elements, and exactly +one element of 'nums' is repeated 'n' times. return the element that is +repeated 'n' times. +""" + + +class Solution(object): + def repeatedNTimes(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + return int((sum(nums) - sum(set(nums))) // (len(nums) // 2 - 1)) + + +if __name__ == "__main__": + obj = Solution() + print(obj.repeatedNTimes(nums=[1, 2, 3, 3])) + print(obj.repeatedNTimes(nums=[2, 1, 2, 5, 3, 2])) + print(obj.repeatedNTimes(nums=[5, 1, 5, 2, 5, 3, 5, 4]))