Web TippsUse custom web fonts in Google Sheets charts(08.09.2026 um 17:05 Uhr)
Web TippsIntroducing the new 1Password App for Google Chat(08.09.2026 um 18:02 Uhr)
Web TippsUse custom web fonts in Google Sheets charts(08.09.2026 um 17:05 Uhr)
Web TippsIntroducing the new 1Password App for Google Chat(08.09.2026 um 18:02 Uhr)

🔧 Programmierung 🕛 vor 2 Monaten 3 Min Lesezeit
0

Remove Duplicates from Sorted Array

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




Problem Statement



Given a sorted integer array nums, remove the duplicates in-place such that each unique element appears only once.



Return the number of unique elements k.



The first k elements of the array should contain the unique elements in their original order.



Example



Input:




CODE
nums = [1,1,2]






Output:




CODE
2






Modified Array:




CODE
[1,2,_]












Brute Force Intuition (Interview Explanation)



One straightforward approach is to use a separate data structure like a HashSet to store unique elements.



As we traverse the array, we insert elements into the set and then copy them back into the original array.



Although simple, it violates the in-place requirement and uses extra memory.






Time Complexity






CODE
O(N)









Space Complexity






CODE
O(N)









Brute Force Java






CODE
class Solution {
public int removeDuplicates(int[] nums) {

HashSet<Integer> set = new HashSet<>();

for (int num : nums) {
set.add(num);
}

int index = 0;

for (int num : set) {
nums[index++] = num;
}

return set.size();
}
}












Moving Towards Optimal



Since the array is already sorted, all duplicate values will appear together.



This means we do not need a HashSet.



We can maintain one pointer for the position of the last unique element and another pointer to explore the array.



Whenever we find a new unique value, we place it next to the previous unique value.



This gives an in-place solution with constant extra space.









Optimal Approach – Two Pointers






Algorithm




  1. Keep pointer i at the last unique element.

  2. Traverse the array using pointer j.

  3. If nums[j] is different from nums[i]:


    • Move i forward.

    • Place nums[j] at index i.



  4. At the end, unique elements occupy positions 0 to i.

  5. Return i + 1.






Why Two Pointers Work



Because the array is sorted:




CODE
1 1 1 2 2 3 4 4






All duplicates are adjacent.



So comparing the current element with the last unique element is enough to detect duplicates.









Optimal Java Solution






CODE
class Solution {
public int removeDuplicates(int[] nums) {

int i = 0;

for (int j = 1; j < nums.length; j++) {

if (nums[j] != nums[i]) {
i++;
nums[i] = nums[j];
}
}

return i + 1;
}
}












Dry Run



Input:




CODE
nums = [1,1,2,2,3,4,4]






Initial:




CODE
i = 0
j = 1









Step 1






CODE
nums[j] = 1
nums[i] = 1

Duplicate found









CODE
i = 0









Step 2






CODE
nums[j] = 2
nums[i] = 1

Unique element found









CODE
i = 1
nums[1] = 2






Array:




CODE
[1,2,2,2,3,4,4]









Step 3






CODE
nums[j] = 2
nums[i] = 2

Duplicate









Step 4






CODE
nums[j] = 3
nums[i] = 2

Unique









CODE
i = 2
nums[2] = 3






Array:




CODE
[1,2,3,2,3,4,4]









Step 5






CODE
nums[j] = 4
nums[i] = 3

Unique









CODE
i = 3
nums[3] = 4






Final Array:




CODE
[1,2,3,4,...]






Return:




CODE
i + 1 = 4












Pattern Recognition



This pattern is commonly used when:




  • Array is sorted

  • Duplicates need to be removed in-place

  • Stable ordering must be preserved

  • Constant extra space is required



Keywords that should trigger this pattern:




CODE
Sorted Array
In-place Modification
Remove Duplicates
Unique Elements






Think:




CODE
Two Pointers
Slow Pointer = Last Valid Position
Fast Pointer = Explorer












Interview One-Liner



Since the array is sorted, duplicates appear consecutively. Using a slow pointer to track the last unique element and a fast pointer to scan the array allows us to overwrite duplicates in-place, achieving O(N) time and O(1) space.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
Use custom web fonts in Google Sheets charts
2 Quellen
Introducing the new 1Password App for Google Chat
1 Quelle
Context-aware access controls are available for Gemini Enterprise in the Admin console
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Remove Duplicates from Sorted Array

Thematisch verwandte Begriffe: Remove, Duplicates, from, Sorted · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...