Advertisement
961. N-Repeated Element in Size 2N Array
UnknownView on LeetCode
Time: O(n)
Space: O(n)
Approach
Use a HashSet; the first element that fails insertion is the repeated element.
961.cs
C#
// Approach: Use a HashSet; the first element that fails insertion is the repeated element.
// Time: O(n) Space: O(n)
public class Solution
{
public int RepeatedNTimes(int[] nums)
{
// Create a HashSet to store unique elements
// Initial capacity is set to n/2 + 1 for optimization
HashSet<int> seenNumbers = new HashSet<int>(nums.Length / 2 + 1);
// Iterate through the array
for (int i = 0; ; ++i)
{
// Try to add current element to the set
// Add() returns false if element already exists
if (!seenNumbers.Add(nums[i]))
// Found the duplicate element that appears n times
return nums[i];
}
}
}Advertisement
Was this solution helpful?