Removing Element
For this blog I’ll be going over a popular LeetCode problem in which we have to delete/replace elements in an array. Here’s a picture of the prompt:
The question is asking us to create a function that takes in an array (nums
) and an integer (val
) and remove all occurrences of val
inside of nums
. There are multiple ways in which we can solve this problem. I chose to solve this problem by using a for
loop and implementing the built-in method .splice()
. I believe it’s important to plan out the steps before doing any actual code! Here’s the pseudo code that I did:
Now that I provided the pseudo code here’s how I solved the problem:
Here are the results on Leetcode:
Please feel free to reach out if there are better methods to solve this problem! For more info on .splice()
, and for
loops,check out the resource section below.