-
Notifications
You must be signed in to change notification settings - Fork 5
/
RemoveDuplicatesFromSortedArray.cs
61 lines (57 loc) · 2.37 KB
/
RemoveDuplicatesFromSortedArray.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LeetCodeSolutionsLib
{
/// <summary>
/// 26. Remove Duplicates from Sorted Array
/// Given a sorted array 'nums', remove the duplicates in-place such that each element appear only once and return the new length.
/// Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
/// IE) Input : nums = [1,1,2]
/// Output : length = 2, with the first two elements of nums being 1 and 2
/// </summary>
public class RemoveDuplicatesFromSortedArray : Solution
{
private int[] _nums;
public RemoveDuplicatesFromSortedArray(int[] nums)
{
_nums = nums;
}
private int removeDuplicates(int[] nums)
{
//Use 2 pointers, set i = 0, j = 0;
//Loop through nums.Length - 1.
// Compare num[i] and num[i+1] (Compare the current element and the next)
// If they are NOT equal to each other, then this is the first occurrence of a new element, therefore we
// Set num[j] = nums[i] (which atm j = 0), then j++
// Else do nothing, since this is a duplicate. Go to the next iteration in the loop
//At the end, set nums[j] = nums[nums.Length - 1] We do this to make sure to include the last element which is not a duplicate
//return j
if (nums.Length <= 0)
{
return 0;
}
int j = 0;
for (int i = 0; i < nums.Length - 1; i++)
{
if (nums[i] != nums[i + 1])
{
nums[j++] = nums[i];
}
}
nums[j++] = nums[nums.Length - 1];
return j;
}
public override void PrintExample()
{
var watch = System.Diagnostics.Stopwatch.StartNew();
var results = removeDuplicates(this._nums);
watch.Stop();
Console.WriteLine($"26. Remove Duplicates From a Sorted Array \n" +
$"Input String = {this.printInputArray(_nums)} \n" +
$"Longest SubString: [{results}] \n" +
$"Execution Speed: {watch.ElapsedMilliseconds}ms \n");
}
}
}