From e146f8b16ec95d432477878b03a3efef1e810a52 Mon Sep 17 00:00:00 2001 From: Suvam Routray <72883939+RayOPnub@users.noreply.github.com> Date: Wed, 27 Oct 2021 10:42:37 +0530 Subject: [PATCH] Remove Duplicates from Sorted Array --- .../removeDuplicates | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Practice Problems/Arrays/Remove Duplicates from Sorted Array/removeDuplicates diff --git a/Practice Problems/Arrays/Remove Duplicates from Sorted Array/removeDuplicates b/Practice Problems/Arrays/Remove Duplicates from Sorted Array/removeDuplicates new file mode 100644 index 0000000..66a1932 --- /dev/null +++ b/Practice Problems/Arrays/Remove Duplicates from Sorted Array/removeDuplicates @@ -0,0 +1,28 @@ +class Solution +{ + public static int removeDuplicates(int[] nums) + { + //check if the array has more than one element or not + if (nums.length < 2) + { + return nums.length; + } + int a = 0; + int b = 1; + + //while loop to check for duplicates + while (b < nums.length) + { + //if loop to check if two elements are same or not + if (nums[b] != nums[a]) + { + a++; + nums[a] = nums[b]; + } + + b++; + } + + return a + 1; + } +} \ No newline at end of file