Keep At Most Two Copies in a Sorted Array
Updated
Problem statement
You are given an integer array nums sorted from smallest to largest. Remove extra copies so that every value appears at most twice, keeping the remaining values in their original order.
Return the kept values as an array. Try to do the work inside nums itself with only a few extra variables, as you would if the array could not be copied; the judge checks only the returned values.
Function
keepAtMostTwo(nums: int[]) → int[]Examples
Example 1
nums = [-4,-4,-4,0,3,3,3,3,8]return = [-4,-4,0,3,3,8]-4 appears three times and 3 appears four times; each keeps two copies. 0 and 8 appear once and stay. Result: [-4,-4,0,3,3,8].
Example 2
nums = [2,5,9]return = [2,5,9]No value appears more than twice, so nothing is removed.
Example 3
nums = [6,6,6,6,6]return = [6,6]Only two copies of 6 are kept.
Constraints
1 <= nums.length <= 2000-10^4 <= nums[i] <= 10^4numsis sorted from smallest to largest
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Subarray Product Less Than KApple · PHONE SCREENApple interview questions
- 3SumAmazon · PHONE SCREEN
- Subarray Product Less Than KAngel One · PHONE SCREEN
- Subarray Product Less Than KThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Trapping Rain WaterGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Sort Ones, Twos, and Threes In PlaceAgoda · PHONE SCREEN · ONSITE INTERVIEWAgoda interview questions
- Candy Crush Grid Matching and GravityRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Find All Pythagorean TripletsDell Technologies · PHONE SCREENDell Technologies interview questions
- Meeting Rooms IIUber · PHONE SCREENUber interview questions
- Sort a Quadratic-Transformed ArrayLinkedIn · PHONE SCREENLinkedIn interview questions