Build a Sorted, Deduplicated List
Practice using a dynamic list (ArrayList/List) to collect, dedupe, and sort values.
Given an array of integers nums, build a new list containing each distinct value from nums exactly once, then sort that list in ascending order and return it.
This is a basics exercise: the point isn't a clever algorithm, it's practicing the everyday operations you'll use constantly in interviews — creating a dynamic list, checking whether it already contains a value, adding to it, and sorting it.
Example 1
Input: nums = [3,1,2,3,1]
Output: [1,2,3]
Explanation: Distinct values 1, 2, 3, sorted ascending.
Example 2
Input: nums = [5,5,5]
Output: [5]
Example 3
Input: nums = []
Output: []
Constraints
- 0 <= nums.length <= 1000
- -10^4 <= nums[i] <= 10^4
Follow-up
Can you dedupe using a HashSet instead of scanning the list, to make each check O(1) instead of O(n)?
Hints
Companies
No companies reported yet.
Discussion
Sign in to join the discussion.
Loading discussion...
Test results
No test cases yet.