Remove Duplicates from Sorted Array in Go — Two Pointers & More

· 2 min read

⚡ TL;DR

Solve Remove Duplicates from Sorted Array in Go with working code. Two Pointers (Optimal) approach with O(n) time, plus complexity analysis and interview tips.

Remove Duplicates from Sorted Array (#26 — Easy): Given a sorted integer array nums, remove duplicates in-place such that each unique element appears only once. Return the number of unique elements.

Problem Statement

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

Example:

Copy
Input: nums = [1,1,2]
Output: 2, nums = [1,2,_]

Approach: Two Pointers (Optimal)

Time: O(n) | Space: O(1)

One read pointer scans all elements; one write pointer only advances when it finds a new unique value. Since the array is sorted, duplicates are always adjacent.

Copy
func removeDuplicates(nums []int) int {
	if len(nums) == 0 { return 0 }
	write := 1
	for read := 1; read < len(nums); read++ {
		if nums[read] != nums[read-1] {
			nums[write] = nums[read]
			write++
		}
	}
	return write
}

func main() {
	nums := []int{1,1,2,2,3}
	k := removeDuplicates(nums)
	fmt.Println(k, nums[:k])  // 3 [1 2 3]
}

Key Takeaways

  • Start with the brute-force approach to understand the problem
  • Two Pointers (Optimal) gives the optimal O(n) solution
  • Practice this pattern — it appears frequently in coding interviews

FAQ

What is the best approach to solve Remove Duplicates from Sorted Array in Go?

The recommended approach is Two Pointers (Optimal), which runs in O(n) time with O(1) space. The full Go implementation is shown above.

What is the time complexity of Remove Duplicates from Sorted Array in Go?

Using Two Pointers (Optimal), the time complexity is O(n) and the space complexity is O(1).

Happy coding!

Related Recommended Services
Visual Studio Code for the Web

Visual Studio Code for the Web

Build with Visual Studio Code, anywhere, anytime, in your browser.

IDEVISUAL STUDIOVISUAL STUDIO CODEWEB
Renovate | Automated Dependency Updates

Renovate | Automated Dependency Updates

Renovate Bot keeps source code dependencies up-to-date using automated Pull Requests.

AUTOMATED DEPENDENCY UPDATESBUNDLERCOMPOSERGITHUBGO MODULES
Best XML Formatter and XML Beautifier

Best XML Formatter and XML Beautifier

Online XML Formatter will format xml data, helps to validate, and works as XML Converter. Save and Share XML.

XMLXML BEAUTIFIERXML CONVERTERXML FORMATXML FORMATTER
Kubecost | Kubernetes cost monitoring and management

Kubecost | Kubernetes cost monitoring and management

Kubecost started in early 2019 as an open-source tool to give developers visibility into Kubernetes spend. We maintain a deep commitment to building and supporting dedicated solutions for the open source community.

CLOUDKUBECOSTKUBERNETESOPEN SOURCESELF HOSTED
Related Recommended Stories
How GitHub reduced testing time for iOS apps with new runner features

How GitHub reduced testing time for iOS apps with new runner features

Learn how GitHub used macOS and Apple Silicon runners for GitHub Actions to build, test, and deploy our iOS app faster.

IOSGITHUBTESTINGRUNNER
5 ways to transform your workflow using GitHub Copilot and MCP

5 ways to transform your workflow using GitHub Copilot and MCP

Learn how to streamline your development workflow with five different MCP use cases.

AGENT MODECODING AGENTCOPILOTFIGMAGITHUB
One weird trick for powerful Git aliases

One weird trick for powerful Git aliases

Advanced Git Aliases

ALIASALIAS TEMPLATEATLASSIANBITBUCKETGIT
Awesome Python

Awesome Python

An opinionated list of awesome Python frameworks, libraries, software and resources

AWESOMEAWESOME PYTHONCOLLECTIONSGITHUBPYTHON
Related Recommended Tools
Find out what websites are built with - Wappalyzer

Find out what websites are built with - Wappalyzer

Find out the technology stack of any website. Create lists of websites and contacts by the technologies they use.

ADD ONSANALYTICSAPP STOREAPPLEBOOKING
Sourcetree | Free Git GUI for Mac and Windows

Sourcetree | Free Git GUI for Mac and Windows

A Git GUI that offers a visual representation of your repositories. Sourcetree is a free Git client for Windows and Mac.

GITGITHUBGITLABATLASSIANBITBUCKET
Related Recommended Videos