LeetCode #27 – Remove Element

This is the next problem:

Even though this problem is labeled as ‘Easy’, the instructions may be quite overwhelming, and at first glance, this problem seems a lot harder than it actually is. Here are examples of output and explanations for your ease of understanding:

Despite it seeming complicated, it is actually quite easy to code. This is why it is labeled as an ‘Easy’ problem. As long as you do not get thrown off by the essay of instructions, it should be simple to code. Here is how I did it:

I’ll break it down for you. The count integer is to count how many indices without val are in the array nums. Thus the for loop and if statement inside it. You also move every value that is not val to the front of the array with the next line, which also increases count by one each time. Here is my output:

Here are the logistics of my solution:

I am very happy with my results for this problem, and it confirms that my way was very efficient. If you have any suggestions, be sure to let me know in the comments section below. I hope you guys at least learned something today and that you all have a great day!

By:

Posted in:


Leave a comment