Data Structures and Algorithms (Day-2 #I4G10DaysOfCodeChallenge)
Remove Element Challenge
Table of contents
Hello!!! Today's challenge is the Remove Element Challenge on Leetcode. Like we always do, whenever we are to solve a coding question/challenge, understanding the question is the first step. Let's go straight to understanding the question.
Question:
Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed.
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.
Return k after placing the final result in the first k slots of nums.
Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
Click Here to view the question from leetcode
Explanation:
This challenge simply implies that if given an array of integers in any particular order and an integer ,say array=[...], where array[i] are integers value=1,2,3.... where k is an integer, the program is required to run such that for each of the array items, array[i], that matches the integer, value, that array item is removed from array in-place. In-place simply means as the program process progresses from one array item to another in the order of the array indexes. Also, it is required that the input array is onlyu modified and not replaced with another array. Basically, this is just what is required. So, let's go into the solutions
Solutions:
When solving this problem, I used two methods of which both worked well. However, one runs faster and uses lower memory. Let's start with solution 1.
Solution 1:
The solution just requires the use of 2 for loops. Here are the steps:
- A function to accommodate the logic is created already by leetcode, although, you can create yours. The removeElement function is created.
- Next, use a for loop to loop through the items of the array. Inside the for loop, an if statement is used to specify some conditions so that the items that matches the given integer is removed. I removed the matching item using the array.splice() method. You can use any method of your choice to remove yours.
- After removing the matching items, there is a chance of some remaining matching items which are not required. This is where the second for loop is introduced. The second for loop is just a duplication of the first, also having the same if statement, condition and item removal (e.g through splicing).
- The removeElement function then returns an array of items that do not match the given integer. Below is a description
var removeElement = (array, value) {
for (....) {
if (each array item is equal to value) {
//splicing operation to remove matching item
}
}
for (...) {
if (each array item is equal to value) {
//splicing operation to remove matching item
}
}
//log the modified array
}
Solution 2:
This solution actually run at a faster speed and uses lower memory. It requires just one for loop. Here are the steps:
- A function to accommodate the logic is created already by leetcode, although, you can create yours. The removeElement function is created.
- Next, use a for loop to loop through the items of the array. Inside the for loop, an if statement is used to specify some conditions so that the items that matches the given integer is removed. I removed the matching item using the array.splice() method. You can use any method of your choice to remove yours.
- After removing the immediate match element, the index of the for loop is reduced/subtracted by 1. The decrement of the loop's index will further remove other matching items ,if any. 4.The removeElement function then returns an array of items that do not match the given integer. Below is a description:
var removeElement = function(array,value){
for (...) {
if (each array item is equal to value) {
//splicing operation to remove matching item
//for loop index decreases by one
}
}
//log the modified array
}
Thanks for Reading.