generated from eyamenko/dotnet-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem37.cs
More file actions
29 lines (26 loc) · 855 Bytes
/
Problem37.cs
File metadata and controls
29 lines (26 loc) · 855 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
namespace LeetCode;
/// <summary>
/// <see href="https://leetcode.com/problems/contains-duplicate/">Contains Duplicate</see>.
/// </summary>
public static class Problem37
{
/// <summary>
/// Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
/// Time complexity: O(n).
/// Space complexity: O(n).
/// </summary>
/// <param name="nums">Array to traverse.</param>
/// <returns>True, if any value appears at least twice in the array.</returns>
public static bool ContainsDuplicate(int[] nums)
{
var set = new HashSet<int>(nums.Length);
for (var i = 0; i < nums.Length; i++)
{
if (!set.Add(nums[i]))
{
return true;
}
}
return false;
}
}