Rearrange Array by Removing Distinct Values - Solution & Explanation
Easy1 min read
Practice this problem
Problem Statement
You are given an integer array nums.
You start with an empty array ans. Repeat the following operation until nums is empty:
- Identify all distinct values currently present in
nums. - Remove one occurrence of every distinct value currently in
nums, and append those values toansin ascending order.
Return the array ans.
Example 1:
Input: nums = [3,1,3,2,1,3]
Output: [1,2,3,1,3,3]
Explanation:
| Operation | Appended to ans |
nums after |
ans after |
|---|---|---|---|
| 1 | 1, 2, 3 | [3, 1, 3] |
[1, 2, 3] |
| 2 | 1, 3 | [3] |
[1, 2, 3, 1, 3] |
| 3 | 3 | [] |
[1, 2, 3, 1, 3, 3] |
nums is now empty, so the answer is [1, 2, 3, 1, 3, 3].
Example 2:
Input: nums = [7,7,4,4,4]
Output: [4,7,4,7,4]
Explanation:
| Operation | Appended to ans |
nums after |
ans after |
|---|---|---|---|
| 1 | 4, 7 | [7, 4, 4] |
[4, 7] |
| 2 | 4, 7 | [4] |
[4, 7, 4, 7] |
| 3 | 4 | [] |
[4, 7, 4, 7, 4] |
nums is now empty, so the answer is [4, 7, 4, 7, 4].
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 100
Solutions for this problem are being prepared.
Try solving it yourselfVideo Solution
4065. Rearrange Array by Removing Distinct Values (Leetcode Easy) • Programming Live with Larry • 140 views views
Watch 2 more video solutions →Ready to solve this problem?
Practice Rearrange Array by Removing Distinct Values with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
DifficultyEasy
Acceptance73.8%
Approaches0
Reading time1 min
Table of Contents
Practice this problem
Open in Editor