educative.io

Solution with TreeMap with similar to the solution with HashMap

Can you please explain what difference does it make if we use TreeMap, instead of HashMap? I wrote the solution with LinkedHashMap as it maintains the order and I don’t need to iterate through the array to find the first element, I just simple return first element of LinkedHashMap which has count 0.

Hi @Nirav ,

This is Fatimah Abdullah from Educative. I noticed your feedback here, and I’m glad you reached out to us.

In response to your feedback, the solutions listed in the lesson are identical. We will remove one of them in the future as we see it has been causing confusion. Thank you for helping with that. The solution you have mentioned using LinkedHashMap is absolutely correct. You have identified a great use case for an ordered HashMap. The only caveat is that I believe the performance might be slightly lower. However, we will consider adding this solution to the challenge in the lesson.

Your feedback is greatly appreciated! Thank you.

Best Regards,
Fatimah Abdullah | Developer Advocate
Educative Inc.

1 Like

Thank you Fatimah for considering my suggestions. :slight_smile:

Ya was also a bit confused by this. I also used a LinkedHashMap since it maintains the order of insertion.

The LinkedHashMap has better prefromance than the TreeMap.

The TreeMap solution should be O(n * logn)
This implementation(TreeMap) provides guaranteed log(n) time cost for the containsKey, get, put and remove operations.

2 Likes

That’s what I thought too. time complexity for TreeMap should be O (n log n).

1 Like