From 196a8b221823659692aa2279f879bcc037f1c490 Mon Sep 17 00:00:00 2001 From: Mahmoud Abdelaal Date: Fri, 31 May 2024 19:45:25 +0300 Subject: [PATCH] 26. Remove Duplicates from Sorted Array --- .../Solution.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/Top Interview 150-leetcode/easy/26. Remove Duplicates from Sorted Array/Solution.java b/Top Interview 150-leetcode/easy/26. Remove Duplicates from Sorted Array/Solution.java index 67e01a3..7ab5180 100644 --- a/Top Interview 150-leetcode/easy/26. Remove Duplicates from Sorted Array/Solution.java +++ b/Top Interview 150-leetcode/easy/26. Remove Duplicates from Sorted Array/Solution.java @@ -4,17 +4,13 @@ class Solution { public int removeDuplicates(int[] nums) { - LinkedHashSet hashSet=new LinkedHashSet<>(); - for (int i = 0; i < nums.length; i++) { - hashSet.add(nums[i]); - } - int index=0; - for (Integer hasset : hashSet) { - nums[index]=hasset; - index++; + int index=1; + for (int i = 1; i < nums.length; i++) { + if (nums[i]!=nums[i-1]) { + nums[index++]=nums[i]; } - return index; - + } + return index; } } \ No newline at end of file