-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path287.FindDuplicate.cs
70 lines (60 loc) · 1.98 KB
/
287.FindDuplicate.cs
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// 287. Find the Duplicate Number
// Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.
// There is only one repeated number in nums, return this repeated number.
// You must solve the problem without modifying the array nums and uses only constant extra space.
// Example 1:
// Input: nums = [1,3,4,2,2]
// Output: 2
public class Solution {
public int FindDuplicate(int[] nums) {
HashSet<int> set = new HashSet<int>();
for(int i = 0; i < nums.Length; i++){
//If the number is already in the set, return it
if(!set.Add(nums[i]))
return nums[i];
}
return -1;
}
}
public class Solution {
public int FindDuplicate(int[] nums) {
int[] freq = new int[nums.Length];
for(int i = 0;i<nums.Length;i++){
freq[nums[i]]++;
}
for(int i = 0;i<freq.Length;i++){
if(freq[i] >1)
return i;
}
return 0;
}
}
public class Solution {
public int FindDuplicate(int[] nums) {
//Move the elements to their respective indices
while(nums[nums[0]]!=nums[0])
(nums[0], nums[nums[0]]) = (nums[nums[0]], nums[0]);
//Return the element at the first index as it is the duplicate
return nums[0];
}
}
public class Solution {
public int FindDuplicate(int[] nums) {
//Floyd's Tortoise and Hare (Cycle Detection)
int fast = nums[0], slow = nums[0];
//Find the intersection point of the two pointers
do{
slow = nums[slow];
fast = nums[nums[fast]];
} while(slow != fast);
//Find the entrance to the cycle
fast = nums[0];
//Move the two pointers at the same speed until they meet
while(slow != fast){
slow = nums[slow];
fast = nums[fast];
}
//Return the duplicate element. It is the entrance to the cycle.
return slow;
}
}