Build with Deegii 🛠️
Build with Deegii 🛠️
/
80. Remove Duplicates from Sorted Array II
Search
80. Remove Duplicates from Sorted Array II

80. Remove Duplicates from Sorted Array II

Tags
Leetcode
Coding
Problem solving
C#
Published
April 3, 2024
Author
Delgersaikhan

Intuition

By maintaining two pointers - one to keep track of the position where the next non-duplicate element should be placed (k), and another to keep track of the count of occurrences of the current element (lcount) - we can efficiently iterate through the array and identify which elements need to be included in the modified array.

Approach

In this problem, we are tasked with removing duplicate elements from a sorted array while allowing at most two occurrences of each element to remain. To achieve this, we can utilize a two-pointer approach where one pointer (k) is used to keep track of the position where the next non-duplicate element should be placed. We also maintain a count of the occurrences of the current element (lcount) and update it accordingly as we iterate through the array.

Complexity

  • Time complexity:O(n)O(n)O(n)
  • Space complexity:O(1)O(1)O(1)

Code

public class Solution { public int RemoveDuplicates(int[] nums) { int lcount = 1; int last = nums[0]; int k = 1; for(int i = 1; i<nums.Length; i++) { lcount = last == nums[i]? lcount + 1 : 1; if(lcount <= 2) nums[k++] = nums[i]; last = nums[i]; } return k; } }
 
Blog Posts
Why I Left Software Engineering for PC Sales (And I'm Still Writing Code)
Why I Left Software Engineering for PC Sales (And I'm Still Writing Code)
I quit software engineering. “Here is Why”
May 24, 2026
Business
Thought Experiments
Tech
Career
Prebuilt PC
Gaming
Энгийн дэлгүүр ч бас инженерийн асуудлаар дүүрэн
Энгийн дэлгүүр ч бас инженерийн асуудлаар дүүрэн
Career
Startups
Gaming
Prebuilt PC
Business
Problem solving
Coding
Software Development
Replacing Prisma Accelerate with Redis and Twemproxy
Replacing Prisma Accelerate with Redis and Twemproxy
Learn how we replaced Prisma Accelerate with a self-hosted Redis and Twemproxy solution, maintaining performance while cutting costs to nearly zero. This practical guide covers building a custom Docker setup, implementing reliable Redis connections in serverless environments, and creating an architecture that seamlessly supports multi-tenancy.
Node.js
Computer Science
Startups
SaaS
Web Dev
Software Development
Projects
Thought Experiments
Research
Coding
Programming
Problem solving
Went on business trip to China
Went on business trip to China
A Note on a business trip to China. Meeting several Chinese biggest refrigerator, freezer manufacturing companies
Jan 18, 2025
Business
Trip
Startups
Career
Projects
Thought Experiments
Tech
Creating a Barcode Detector Using the Browser BarcodeDetector API
🤳
Creating a Barcode Detector Using the Browser BarcodeDetector API
In this tutorial, we'll create a simple barcode detector web application using the Browser BarcodeDetector API. The app will access the user's webcam, display the video stream, and detect barcodes in real-time.
Jun 17, 2024
Javascript
Computer Science
Video
Web Dev
Software Development
App Dev
Thought Experiments
Tech
Coding
Programming
45. Jump Game II
45. Jump Game II
Solve leetcode 45. Jump Game II using greedy approach.
Apr 15, 2024
C#
Leetcode
Coding
Programming
Problem solving
Competitive programming
Greedy Algorithm
122. Best Time to Buy and Sell Stock II
122. Best Time to Buy and Sell Stock II
Solve leetcode 122. Best Time to Buy and Sell Stock II using greedy algorithm
Apr 10, 2024
C#
Leetcode
Coding
Programming
Problem solving
Competitive programming
121. Best Time to Buy and Sell Stock
121. Best Time to Buy and Sell Stock
Solve leetcode 121. Best Time to Buy and Sell Stock using simple greedy algorithm.
Apr 9, 2024
C#
Leetcode
Coding
Programming
Problem solving
Competitive programming
169. Majority Element
169. Majority Element
Solve leetcode 169. Majority Element with sorting method.
Apr 9, 2024
C#
Leetcode
Coding
Programming
Problem solving
Competitive programming
26. Remove Duplicates from Sorted Array
26. Remove Duplicates from Sorted Array
Solving leetcode problem 26. Remove duplicates from sorted array using two pointer approach.
Apr 8, 2024
C#
Leetcode
Coding
Competitive programming
Problem solving
27. Remove Element
27. Remove Element
Leetcode (Remove Element) problem solution using two pointers.
Apr 8, 2024
Leetcode
Coding
Problem solving
Competitive programming
C#
Computer Science
Building your portfolio website backed by Notion as CMS
Building your portfolio website backed by Notion as CMS
Publish your portfolio notion page to the web under 30 minutes using Next.js.
Apr 2, 2024
Node.js
Problem solving
Lifestyle
Programming
Web Dev
1768. Merge strings Alternately (Beats 96.66%)
⚙️
1768. Merge strings Alternately (Beats 96.66%)
2 pointer approach for solving a problem that merges two strings alternately.
Mar 29, 2024
C#
Leetcode
Computer Science
Coding
Programming
Problem solving
Empowering legacy KonvaJS in React projects
Building visual editor tools with Konva and React.
Coding
Problem solving
Node.js
React.js
Table of Contents
IntuitionApproachComplexityCode
Copyright 2026 Delgersaikhan Bayanmunkh