FlareStart
HomeNewsHow ToSources
FlareStart

Where developers start their day. All the tech news & tutorials that matter, in one place.

Quick Links

  • Home
  • News
  • Tutorials
  • Sources
  • Privacy Policy

Connect

© 2026 FlareStart. All rights reserved.

Back to articles
Remove Duplicates from Sorted Array II (LeetCode 80) — Simple Explanation
NewsProgramming Languages

Remove Duplicates from Sorted Array II (LeetCode 80) — Simple Explanation

via Dev.to BeginnersNithya Dharshini official2h ago

This problem is a great extension of the basic two-pointer technique. Problem Understanding We are given a sorted array. -> We must remove duplicates in-place -> But each element can appear at most twice Example Input: -> [1,1,1,2,2,3] Output: -> [1,1,2,2,3] Return: -> k = 5 Idea (Very Simple) We use two pointers: j -> reads elements k -> writes valid elements Key Condition if (k < 2 || nums[j] != nums[k - 2]) What does this mean? First 2 elements are always allowed After that: Compare current element with 2 positions before If same -> skip If different -> include Why k - 2? Because we allow maximum 2 duplicates so we check “Did we already take this number twice?” C++ Code class Solution { public: int removeDuplicates ( vector < int >& nums ) { int k = 0 ; for ( int j = 0 ; j < nums . size (); j ++ ) { if ( k < 2 || nums [ j ] != nums [ k - 2 ]) { nums [ k ] = nums [ j ]; k ++ ; } } return k ; } }; Dry Run (Important) Input: -> [1,1,1,2,2,3] j nums[j] k Action Result 0 1 0 take [1] 1 1

Continue reading on Dev.to Beginners

Opens in a new tab

Read Full Article
0 views

Related Articles

The Pentagon is developing alternatives to Anthropic, report says
News

The Pentagon is developing alternatives to Anthropic, report says

TechCrunch • 22m ago

Best early Amazon Spring Sale 2026 smartwatch and smart ring deals
News

Best early Amazon Spring Sale 2026 smartwatch and smart ring deals

ZDNet • 24m ago

Why Some Developers Keep Growing While Others Fall Behind
News

Why Some Developers Keep Growing While Others Fall Behind

Medium Programming • 54m ago

These Sonos Over-Ear Headphones Are $100 Off
News

These Sonos Over-Ear Headphones Are $100 Off

Wired • 57m ago

Best Walmart deals to compete with Amazon's Big Spring Sale 2026
News

Best Walmart deals to compete with Amazon's Big Spring Sale 2026

ZDNet • 1h ago

Discover More Articles