Exercises
We will resolve one exercise from LeetCode using the set data structure to remove duplicate values from an array.
Remove duplicates from sorted array
The exercise we will resolve the is the 26. Remove Duplicates from Sorted Array problem available at https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/.
When resolving the problem using JavaScript or TypeScript, we will need to add our logic inside the function function removeDuplicates(nums: number[]): number
, which receives an array of numbers and expects the number of unique elements within the array as a return. For our solution to be accepted, we also have to remove the duplicates from the nums
array in-place, meaning we cannot simply assign a new reference to it.
Let's write the removeDuplicates
function using a set data structure to easily remove the duplicates from the array:
export function removeDuplicates2(nums: number[]): number {
const set = new Set(nums);
const arr = Array.from...