This content originally appeared on DEV Community and was authored by Giuseppe
Time Complexity O(n)
Space Complexity O(1)
class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length <= 2) return nums.length;
int index = 2;
for (int i = 2; i < nums.length; i++) {
// Keep element if it's different from the element 2 positions back
if (nums[i] != nums[index - 2]) {
nums[index] = nums[i];
index++;
}
}
return index;
}
}
This content originally appeared on DEV Community and was authored by Giuseppe
Print
Share
Comment
Cite
Upload
Translate
Updates
There are no updates yet.
Click the Upload button above to add an update.
APA
MLA
Giuseppe | Sciencx (2025-08-13T10:38:37+00:00) LeetCode #80. Remove Duplicates from Sorted Array II. Retrieved from https://www.scien.cx/2025/08/13/leetcode-80-remove-duplicates-from-sorted-array-ii/
" » LeetCode #80. Remove Duplicates from Sorted Array II." Giuseppe | Sciencx - Wednesday August 13, 2025, https://www.scien.cx/2025/08/13/leetcode-80-remove-duplicates-from-sorted-array-ii/
HARVARDGiuseppe | Sciencx Wednesday August 13, 2025 » LeetCode #80. Remove Duplicates from Sorted Array II., viewed ,<https://www.scien.cx/2025/08/13/leetcode-80-remove-duplicates-from-sorted-array-ii/>
VANCOUVERGiuseppe | Sciencx - » LeetCode #80. Remove Duplicates from Sorted Array II. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/08/13/leetcode-80-remove-duplicates-from-sorted-array-ii/
CHICAGO" » LeetCode #80. Remove Duplicates from Sorted Array II." Giuseppe | Sciencx - Accessed . https://www.scien.cx/2025/08/13/leetcode-80-remove-duplicates-from-sorted-array-ii/
IEEE" » LeetCode #80. Remove Duplicates from Sorted Array II." Giuseppe | Sciencx [Online]. Available: https://www.scien.cx/2025/08/13/leetcode-80-remove-duplicates-from-sorted-array-ii/. [Accessed: ]
rf:citation » LeetCode #80. Remove Duplicates from Sorted Array II | Giuseppe | Sciencx | https://www.scien.cx/2025/08/13/leetcode-80-remove-duplicates-from-sorted-array-ii/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.