DEV Community

Dennar David
Dennar David

Posted on

#10daysofcodechallenge by Ingressive for Good (#Day 2)

I've just begun a 10-day coding challenge organized by Ingressive for Good and I'll be sharing my thoughts on Day 2 of the challenge.

Let me briefly describe how the challenge works before I share my experience with you. From September 21 through October 30, 2022, the challenge is to solve one common algorithm problem every day for ten days.

Today's algorithm problem was "Removing all occurrences of a particular value in an array". It is the 27th algorithm challenge on "Leetcode.com"

I'll now share my experience. Like day 1, this wasn't as difficult. The task was to delete a certain value from an array while it was still in place without creating a new array, and to return the length of the array as it now stood without the removed values.

I chose to use the programming language javaScript to address this issue, I created a "for loop" to help me solve the algorithm. A condition is added when the loop traverses the array.
if(nums[i] === val){
nums.splice(i,1);
i--;
}
.
Using the built-in javaScript function splice, this would find any value that we didn't want to be in the array and remove it. The loop's counter would then be decreased to go on to the next element in the array.

This had a runtime of 78 ms and used roughly 42 megabytes of memory.

Top comments (0)