-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainsDuplicate.cs
More file actions
50 lines (41 loc) · 1.1 KB
/
Copy pathContainsDuplicate.cs
File metadata and controls
50 lines (41 loc) · 1.1 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
namespace Solutions.Problems;
public class ContainsDuplicateSolution
{
public bool ContainsDuplicateBruteForce(int[] nums)
{
for (int x = 0; x < nums.Length; x++)
for (int y = x + 1; y < nums.Length; y++)
if (nums[x] == nums[y])
return true;
return false;
}
public bool ContainsDuplicateSorting(int[] nums)
{
nums.Sort();
for (int x = 1; x < nums.Length; x++)
if (nums[x] == nums[x - 1])
return true;
return false;
}
public bool ContainsDuplicateHashSet(int[] nums)
{
var hashSet = new HashSet<int>();
foreach (int num in nums)
{
if (hashSet.Contains(num))
return true;
hashSet.Add(num);
}
return false;
}
/// <summary>
/// HashSet Length Solution
/// </summary>
/// <param name="nums"></param>
/// <returns></returns>
public bool ContainsDuplicate(int[] nums)
{
var hashSet = nums.ToHashSet();
return hashSet.Count != nums.Length;
}
}