PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Microsoft

Remove duplicates in-place from unsorted array

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of in-place array manipulation, duplicate removal, and algorithmic analysis under strict extra-space constraints. It is commonly asked in Coding & Algorithms interviews to assess reasoning about time-space trade-offs and algorithmic complexity, emphasizing practical application and implementation-level skills rather than purely conceptual knowledge.

  • medium
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Remove duplicates in-place from unsorted array

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Problem You are given an array `nums` of **unsorted** integers. Write a method that **removes duplicates in-place** (i.e., without using any additional data structure such as a hash set/map/boolean array). The method should return the **number of unique elements** `k` and modify `nums` so that the first `k` positions contain the unique values. ## Requirements - Extra space: **O(1)** (ignore call stack). - The input array is **unsorted**. - You may **reorder** the array elements unless otherwise stated. ## Follow-up - Is an **O(n)** time solution possible under these constraints? If yes, state the needed assumptions; if not, explain why. ## Examples - Input: `[3, 1, 3, 2, 1]` → Output `k = 3`, first `k` elements are some permutation of `{1, 2, 3}`. - Input: `[]` → Output `k = 0`. ## Constraints - `0 ≤ n ≤ 10^5` - Integers fit in 32-bit signed range.

Quick Answer: This question evaluates understanding of in-place array manipulation, duplicate removal, and algorithmic analysis under strict extra-space constraints. It is commonly asked in Coding & Algorithms interviews to assess reasoning about time-space trade-offs and algorithmic complexity, emphasizing practical application and implementation-level skills rather than purely conceptual knowledge.

Related Interview Questions

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)
Microsoft logo
Microsoft
Feb 12, 2026, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
7
0
Loading...

Problem

You are given an array nums of unsorted integers.

Write a method that removes duplicates in-place (i.e., without using any additional data structure such as a hash set/map/boolean array). The method should return the number of unique elements k and modify nums so that the first k positions contain the unique values.

Requirements

  • Extra space: O(1) (ignore call stack).
  • The input array is unsorted .
  • You may reorder the array elements unless otherwise stated.

Follow-up

  • Is an O(n) time solution possible under these constraints? If yes, state the needed assumptions; if not, explain why.

Examples

  • Input: [3, 1, 3, 2, 1] → Output k = 3 , first k elements are some permutation of {1, 2, 3} .
  • Input: [] → Output k = 0 .

Constraints

  • 0 ≤ n ≤ 10^5
  • Integers fit in 32-bit signed range.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Microsoft•More Software Engineer•Microsoft Software Engineer•Microsoft Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.