Techletter #31 | May 25, 2023
Solving coding challenges can be tricky. You got to solve many problems to understand even the solution correctly. For most of the problems, you can find solutions online. But, not all solutions you can understand. So, here I have documented a few of the problems with its explanation.
Problem Statement 1: Remove the specified element
Given an integer array nums
and an integer val
, remove all occurrences of val
in nums
in-place. The order of the elements may be changed. Then return the number of elements in nums
which are not equal to val
.
// input array
arr = [1, 1, 3, 4, 3] val = 3
// output
arr = [1, 1, 4, _, _]
Underscores assume to be 0 or null. It’s not mandatory. After you return the length of arr. The elements from arr[0] to arr[length-1] should satisfy the condition. Any elements after that don’t matter.
function removeSpecified(nums, val) {
cosnt n = nums.length;
let count = 0;
// assuming that nums.length is not 0 or 1
for (let i = 0; i < n; i++) {
// skip the element if nums[i] match the val
if (nums[i] == val) {
continue;
}
// add the element only if it don't match
nums[count] = nums[i];
count++;
}
return count;
}
You can also make use of one single while loop with a two-pointer technique. By just swapping the repeating numbers to the last of the array that is if (elementRepeat)
add last element to first & decrement right
else increment left
.
function removeSpecified(nums, val) {
let left = 0;
let right = nums.length;
while (left < right) {
if (nums[left] === val) {
nums[left] = nums[right - 1];
right--;
} else {
left++;
}
}
return right;
}
Problem Statement 2: Remove duplicate elements from a sorted array
Given an integer array nums
sorted in non-decreasing order, remove the duplicates in place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums
.
function removeDuplicates(arr) {
let n = arr.length;
if (n == 0 || n == 1) return n;
let j = 0;
for (let i = 0; i < n - 1; i++) {
if (arr[i] !== arr[i+1]) {
arr[j++] = arr[i]
}
}
// we store last element to arr[j++] because i runs till n-1 only. So it won't insert the last element
arr[j++] = arr[n - 1];
return j;
}
Problem statement 3: Remove duplicates from a sorted array where elements can appear at most twice.
Given an integer array nums
sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same.
It’s similar to the above problem but with a specified condition.
function removeDuplicates(nums) {
let count = 0;
for (let i = 0; i < nums.length; i++) {
if (count < 2 || nums[i] !== nums[count - 2]) {
nums[count] = nums[i];
count++;
}
}
return count;
}
if (count < 2)
because you can have elements repeating at most twice.
nums[i] !== nums[count - 2]
because as elements can appear twice, you should compare nums[i]
two counts before the current count.
You can solve the above problems in various different ways. But, I found these ways to be simple and easy to understand. You can find all these questions on leetcode with question numbers: 27, 26 & 80 respectively.