Created
July 10, 2021 05:34
-
-
Save schauhan232/d5b0772dd685d01d672ef9464f659742 to your computer and use it in GitHub Desktop.
Remove duplicate element from Sorted Array C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var array = new int[] { 0, 0, 1, 1, 1, 2, 2, 3, 3, 4 }; // 1, 1, 2 | |
var LEFT = 0; | |
var RIGHT = 1; | |
var newIndex = 1; | |
while (RIGHT < array.Length + 1) | |
{ | |
if (RIGHT == array.Length) | |
{ | |
//for pushing last item only | |
if (array[newIndex] != array[RIGHT - 1]) | |
{ | |
Console.WriteLine(); | |
array[newIndex] = array[LEFT]; | |
Console.WriteLine($"Value now at { newIndex} = {array[newIndex]}"); | |
} | |
break; | |
} | |
Console.Write($"Left: {array[LEFT]}, right :{array[RIGHT]}"); | |
if (array[LEFT] != array[RIGHT]) | |
{ | |
Console.WriteLine(); | |
array[newIndex] = array[LEFT]; | |
Console.WriteLine($"Value now at { newIndex} = {array[newIndex]}"); | |
newIndex++; | |
} | |
Console.WriteLine(); | |
LEFT++; | |
RIGHT++; | |
} | |
Console.Write("Final Output:"); | |
for (int j = 1; j <= newIndex; j++) | |
{ | |
Console.Write($"{array[j]}"); | |
} | |
Console.ReadKey(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment