Deduplicate a Sorted Array In Place
Updated
Problem statement
You are given an integer array nums sorted in nondecreasing order. Modify it in place so that each distinct value appears exactly once and the retained values stay in sorted order.
Return an array containing exactly the compacted prefix. Use O(1) auxiliary workspace for the compaction; the returned array used to expose that prefix to the judge is not counted as auxiliary workspace.
Function
deduplicateSortedArray(nums: int[]) → int[]Examples
Example 1
nums = [1,1,1,2,2,3,3]return = [1,2,3]The first copy of each distinct value is retained and all later copies are removed.
Example 2
nums = [-2,-2,0,0,0,5]return = [-2,0,5]The compacted sorted prefix contains the three distinct values -2, 0, and 5.
Example 3
nums = []return = []An empty array already contains no duplicates.
Constraints
0 <= nums.length <= 100000.-1000000000 <= nums[i] <= 1000000000.numsis sorted in nondecreasing order.
More GEP interview questions
Similar questions at other companies
- Compact Conversation History by Token BudgetSierra · PHONE SCREENSierra interview questions
- Remove Duplicates from Sorted ArrayNykaa · PHONE SCREENNykaa interview questions
- Minimum Index Distance Between Person and CakeSnowflake · OA · PHONE SCREENSnowflake interview questions
- Squares of a Sorted ArrayInstacart · PHONE SCREENInstacart interview questions
- Closest Vehicle and Charger in a LineSpotify · PHONE SCREEN
- Correct an In-Place Array ReversalDuolingo · PHONE SCREENDuolingo interview questions
- Remove Duplicates From Sorted Array In PlaceKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Merge Two Sorted ArraysCapgemini · PHONE SCREENCapgemini interview questions
- Maximize Distance to the Closest Occupied SeatAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Remove Duplicates From Sorted Array In PlaceGet My Parking · PHONE SCREEN