DEV Community

Find duplicates algorithm series- A SORTED array as an input.

Awdesh on July 17, 2018

This is an extension of problem we have discussed in an earlier post. ...
Collapse
 
karthik19894 profile image
Karthik Ruthiravelu

I just made an inplace algorithm to move the duplicates replaced with zeros to the end of array, this runs in O(n) time and O(1) space, you can find the code for the same here : codepen.io/karthik19894/pen/XWWwbw...

Collapse
 
_hs_ profile image
HS

Try running this array example int[] arr = new int[]{1,2,2,3,4,4,5,6,3, 9, 8, 7,9999}; :D

Collapse
 
s_awdesh profile image
Awdesh

That is not the correct sample data set for this problem as problem statement specifies that input array must be sorted.

Although I realized that repl was pointing to code base of different problem.I have attached a right code base now.

Collapse
 
_hs_ profile image
HS

Yeah I was using repl code example and just changed last element. Was pointing this out because I know some people learning Java will try stuff with the code and be confused about results.

Collapse
 
aliahsan07 profile image
Ali Ahsan

Referring to the example above, wouldn't this give us 4 twice in the duplicates table?

Collapse
 
s_awdesh profile image
Awdesh • Edited

you are right, I should check if item already exists inside the duplicate list. I have updated the code and attached repl.
Thanks for the feedback.