From 9daf3346c4e5ae9511e4d967ad8c42077c376a2b Mon Sep 17 00:00:00 2001 From: Aman Kumar Date: Thu, 3 Jul 2025 14:34:35 +0530 Subject: [PATCH 1/2] Created Remove Duplicate From Sorted Array Implement in-place duplicate removal from sorted array using two-pointer technique. --- .../RemoveDuplicatesFromSortedArray.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/main/java/com/thealgorithms/others/RemoveDuplicatesFromSortedArray.java diff --git a/src/main/java/com/thealgorithms/others/RemoveDuplicatesFromSortedArray.java b/src/main/java/com/thealgorithms/others/RemoveDuplicatesFromSortedArray.java new file mode 100644 index 000000000000..93351dc01f4c --- /dev/null +++ b/src/main/java/com/thealgorithms/others/RemoveDuplicatesFromSortedArray.java @@ -0,0 +1,75 @@ +package com.thealgorithms.others; + +import java.util.Scanner; + +/** + * Remove duplicates from sorted array. + * + *

+ * Given an integer array nums sorted in non-decreasing order, remove the + * duplicates in-place such that each unique element appears only once. + * The relative order of the elements should be kept the same. Then return + * the number of unique elements in nums. + * + *

+ */ +public final class RemoveDuplicatesFromSortedArray { + + private RemoveDuplicatesFromSortedArray() { + // private constructor to prevent instantiation + } + + /** + * Main method with user input. + */ + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + System.out.println("Enter size of sorted array:"); + int n = scanner.nextInt(); + + if (n <= 0) { + System.out.println("Invalid array size."); + scanner.close(); + return; + } + + int[] nums = new int[n]; + System.out.println("Enter " + n + " integers in non-decreasing order:"); + for (int i = 0; i < n; i++) { + nums[i] = scanner.nextInt(); + } + + int k = removeDuplicates(nums); + + System.out.println("Number of unique elements: " + k); + System.out.print("Array after removing duplicates: "); + for (int i = 0; i < k; i++) { + System.out.print(nums[i] + " "); + } + System.out.println(); + + scanner.close(); + } + + /** + * Removes duplicates from a sorted array in-place using O(1) space. + * + * @param nums The input sorted array + * @return The number of unique elements + */ + public static int removeDuplicates(int[] nums) { + if (nums == null || nums.length == 0) { + return 0; + } + + int j = 0; // points to last unique element + for (int i = 1; i < nums.length; i++) { + if (nums[j] != nums[i]) { + j++; + nums[j] = nums[i]; + } + } + return j + 1; + } +} From a983e469708214932bb799e5b6b91c3869c23edc Mon Sep 17 00:00:00 2001 From: Aman Kumar Date: Thu, 3 Jul 2025 14:45:40 +0530 Subject: [PATCH 2/2] Update RemoveDuplicatesFromSortedArray.java --- .../RemoveDuplicatesFromSortedArray.java | 60 ++++--------------- 1 file changed, 13 insertions(+), 47 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/RemoveDuplicatesFromSortedArray.java b/src/main/java/com/thealgorithms/others/RemoveDuplicatesFromSortedArray.java index 93351dc01f4c..2f869c7928bd 100644 --- a/src/main/java/com/thealgorithms/others/RemoveDuplicatesFromSortedArray.java +++ b/src/main/java/com/thealgorithms/others/RemoveDuplicatesFromSortedArray.java @@ -1,75 +1,41 @@ package com.thealgorithms.others; -import java.util.Scanner; - /** - * Remove duplicates from sorted array. - * - *

- * Given an integer array nums sorted in non-decreasing order, remove the - * duplicates in-place such that each unique element appears only once. - * The relative order of the elements should be kept the same. Then return - * the number of unique elements in nums. + * Removes duplicates from a sorted integer array in-place + * using the two-pointer technique and O(1) extra space. * *

+ * Example: + * Input: [1, 1, 2, 2, 3] + * Output: 3, and the array becomes [1, 2, 3, _, _] + *

*/ public final class RemoveDuplicatesFromSortedArray { private RemoveDuplicatesFromSortedArray() { - // private constructor to prevent instantiation - } - - /** - * Main method with user input. - */ - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - - System.out.println("Enter size of sorted array:"); - int n = scanner.nextInt(); - - if (n <= 0) { - System.out.println("Invalid array size."); - scanner.close(); - return; - } - - int[] nums = new int[n]; - System.out.println("Enter " + n + " integers in non-decreasing order:"); - for (int i = 0; i < n; i++) { - nums[i] = scanner.nextInt(); - } - - int k = removeDuplicates(nums); - - System.out.println("Number of unique elements: " + k); - System.out.print("Array after removing duplicates: "); - for (int i = 0; i < k; i++) { - System.out.print(nums[i] + " "); - } - System.out.println(); - - scanner.close(); + // Utility class, no instantiation } /** - * Removes duplicates from a sorted array in-place using O(1) space. + * Removes duplicates from a sorted array in-place. * - * @param nums The input sorted array - * @return The number of unique elements + * @param nums the sorted input array (non-decreasing order) + * @return the number of unique elements */ public static int removeDuplicates(int[] nums) { if (nums == null || nums.length == 0) { return 0; } - int j = 0; // points to last unique element + int j = 0; // Pointer for the last unique element + for (int i = 1; i < nums.length; i++) { if (nums[j] != nums[i]) { j++; nums[j] = nums[i]; } } + return j + 1; } }