150 Javascript problems, copyable for ease of use

Javascript problems

Contains duplicate

Last Save 08212025-0515pm:

[Remove Element - NeetCode](https://neetcode.io/problems/remove-element?list=neetcode150)
  • YALSS - yet another leetcode study system. YALSS gotta try it
  • CTRL+SHIFT+K to duplicate a browser tab

| Number | problem name | Typescript | Python | C# | Problem link | | ------ | ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------ | --- | --------------------------------------------------------------------------------- | | 1 | [[# Remove Duplicates From Sorted Array]] | [[#Remove Duplicates From Sorted Array Sorted Set Javascript]][[# Remove Duplicates From Sorted Array Two Pointers]][[## Remove Duplicates From Sorted Array Two Pointers Approach 2]] | | | https://neetcode.io/problems/remove-duplicates-from-sorted-array?list=neetcode150 | | 2 | [[#Remove Element]] | [[#Remove Element Solution = Brute Force]] | | | https://neetcode.io/problems/remove-element?list=neetcode150 | | 3 | | | | | | | | | | | | |

Remove Duplicates From Sorted Array

You are given an integer array nums sorted in non-decreasing order. Your task is to remove duplicates from nums in-place so that each element appears only once.

After removing the duplicates, return the number of unique elements, denoted as k, such that the first k elements of nums contain the unique elements.

Note:

  • The order of the unique elements should remain the same as in the original array.
  • It is not necessary to consider elements beyond the first k positions of the array.
  • To be accepted, the first k elements of nums must contain all the unique elements.

Return k as the final result.

Example 1:

Input: nums = [1,1,2,3,4]

Output: [1,2,3,4]

Explanation: You should return k = 4 as we have four unique elements.

Example 2:

Input: nums = [2,10,10,30,30,30]

Output: [2,10,30]

Explanation: You should return k = 3 as we have three unique elements.

Constraints:

  • 1 <= nums.length <= 30,000
  • -100 <= nums[i] <= 100
  • nums is sorted in non-decreasing order.

Remove Duplicates From Sorted Array Sorted Set Javascript

Class Solution {
	removeDuplicates (nums) {
	const unique = Array.from(new Set (nums)).sort((a,b) => );
	for (let i = 0; i < unique.length; i++){
		nums[i] = unique[i];
	}
	return unique.length;
	}
}

Time & Space Complexity:

  • Time: O(nlogn)
  • Space: O(n)

Remove Duplicates From Sorted Array Two Pointers

Class Solution {
	removeDuplicates(nums){
		let n = nums.length, 
		l = 0,
		r = 0;
	}
	while (r < n) {
		nums[l] = nums[r]
		while (r < n && nums[r] === nums[l]) {
			r++;
		}
		l++;
	}
	return l;
}

Time Space:

  • O(n)
  • O()

Remove Duplicates From Sorted Array Two Pointers Approach 2

Class Solution {
	removeDuplicates(nums) {
		let l = 1;
		for (let r = 1; r < nums.length; r++) {
			if (nums[r] !== nums[r-1]) {
				nums[l++] = nums[r];
			}
		}
		return l;
	}
}

TimeSpace:

  • O(n)
  • O(1)

Remove Element

You are given an integer array nums and an integer val. Your task is to remove all occurrences of val from nums in-place.

After removing all occurrences of val, return the number of remaining elements, say k, such that the first k elements of nums do not contain val.

Note:

  • The order of the elements which are not equal to val does not matter.
  • It is not necessary to consider elements beyond the first k positions of the array.
  • To be accepted, the first k elements of nums must contain only elements not equal to val.

Return k as the final result.

Example 1:

Input: nums = [1,1,2,3,4], val = 1

Output: [2,3,4]

Explanation: You should return k = 3 as we have 3 elements which are not equal to val = 1.

Example 2:

Input: nums = [0,1,2,2,3,0,4,2], val = 2

Output: [0,1,3,0,4]

Explanation: You should return k = 5 as we have 5 elements which are not equal to val = 2.

Constraints:

  • 0 <= nums.length <= 100
  • 0 <= nums[i] <= 50
  • 0 <= val <= 100

Remove Element Solution = Brute Force

class Solution {
	removeElement(nums, val) {
	const tmp = [];
	for (const num of nums) {
		if (num !== val) {
			tmp.push(num);
		}
	}
	for (let i = 0; i < tmp.length; i++) {
		nums[i] = tmp[i];
	}
	return tmp.length
	}
}

Remove Element Solution = Two Pointers 1

class Solution {
	removeElement(nums, val) {
	const tmp = [];
	for (const num of nums) {
		if (num !== val) {
			tmp.push(num)
			}
		}
	}
	for (let i = 0; i < tmp.length; i++) {
		nums[i] = tmp[i];
	}
	return tmp.length;
}