My Little Experience Solving Leetcode Problem -

Question - Remove Duplicates from Sorted Array

ยท

3 min read

My Little Experience Solving Leetcode Problem -

Hi guys,
Today I want to talk about my experience solving a problem on Leetcode (difficulty level : easy) and how I was able to solve the problem.

image.png

So the problem in summary says

we are to remove all the duplicates from an array nums, which has been sorted in a non increasing format such that only unique elements appear once". Sounds simple right? Well maybe, or maybe not. But either ways, I'll be sharing some hints on how i was able to solve this problem that took me 2 hours!

tenor.gif

So for starters, my first instinct was that we would be needing to run a loop and that we would also need another array to hold the unique elements from the array which we are given.

array  arr = []
[int] i = 0
while(){
  if( array(bla bla bla) contains (bla bla bla)){
  //then do something  }
}

After the loop my sorrows began.๐Ÿ˜…๐Ÿ˜… It became trial after trial, and compilation failure upon failure. Got rid of my code on leetcode and used an online compiler to compile it. It provided the exact correct result i was looking for, which leetcode refused (no be juju be this one?)๐Ÿคฃ.

I decided to clear all my code and follow a different approach using set even when i knew it might be another failed attempt . My trial using set looked something like this

let nums = [0,1];

var arr = new Set(nums);

console.log( blablabla);

and somehow this also gave me the correct result when compiled outside of leetcode . But on puting my code in leetcode, it was a failure.

frustrated-angry.gif

I was frustrated.

Its was 2am in the morning and i had been on a single question for almost 2 hours. Decided to go for one last trial where i would put everything on the line for the next 30 mins ( that was me speaking dramatically lol!)

I followed my first approach using the Loop First then Then Sort Later Method . First by checking adding the unique elements in a different array, i was able to extract the duplicates and give them another character (other than a number). Then came the sorting time . Sorted first sort, a second sort, clicked the Submit button and Vollia!!!

IT WORKED!!!!!!! ๐Ÿ˜ฒ๐Ÿ˜ฒ๐Ÿ˜ฒ๐Ÿ˜ฒ What the hell!

Y'all needed to see how i shouted ๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚. The yoke was finally broken!๐Ÿ˜‚.

image.png Ps: forget about the memory usage. At that time i was only glad i had succeeded in solving the problem.

Anyway, let's fast forward to the present where I'm writing this blog and relishing my experience solving the problem and hoping you got the hints and punchlines I dropped so you can get a solution to the same problem.

Hope you enjoyed this. Let me know what you think in the comment below.

You can also follow me on Twitter and Linkedin

ย