Day 2 of 10: IngressiveForGood 10 Days of Code Challenge

Day 2 of 10: IngressiveForGood 10 Days of Code Challenge

It's day 2 of the 10 Days of Code Challenge by IngressiveForGood and today's task is as follows:

Instructions

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.

My Solution

For this problem, my first approach was to loop through the array and delete the any integer which corresponds to the value given as val. However, this only removed the first occurrence of the value and not all occurences. I had to figure out a way to for the for loop iteration to continue even after the condition is met.

After much research, I figured I was approaching the problem the wrong way. So, I decided to program the for loop to check and leave any element as it is only if the element in the input array was not the value given. However, this still didnt pass all testcases. It didn't eliminate all occurrences of val.

At this point, i couldn't figure out what was wrong. After much trial and error and a very satisfying dinner, i found the problem!. I was using the python shell and not python3. To make my code work in the outdated version, i needed to include the index variable in the range. However, it worked just fine without that addition in the python3 shell. Kudos to those responsible for the upgrade! You all are the real heroes! Making life easier...

You can view my submission below.

day 2 submission.jpg

Than you for reading. Looking forward to Day 3!