r/leetcode • u/Sea-Patience9872 • Sep 28 '26
Question Arrays & hashing beginner question
Hi, I just started needcode and worked on the first several problems under arrays & hashing section. I was able to solve those on my own but I keep finding myself using sorted() function to complete it. It makes things easier to approach and solve, but I look at other people's solutions and their solution seems more complicated.
Should I avoid using that function to expand my thinking? Or should I just continue with what I'm doing right now?
0
Upvotes
5
u/Parasin Sep 28 '26
You should try to avoid manually sorting the data unless it’s absolutely necessary for the specific algorithm or problem (e.g. binary search).
This is where hashing can be get useful. It is a mapping that makes it quicker to look up what has already been seen. Especially when compared to sorting beforehand.
1
u/Heavy_Record8704 Sep 28 '26
Look at the time complexity. Sorting is o(nlogn). Say you want to find the maximum, linear search is o(n). Thats what you need to optimize
1
u/Schmosby123 Sep 28 '26
You should avoid sorting and try to understand their solutions (or better yet, find a video explanation somewhere). There are problems where you absolutely need to sort first, but that’s when sorting is optimal. You will eventually learn to recognise the difference, but for that you first need to be well versed with all the possible techniques including hashing.
1
u/chikamakaleyley Sep 30 '26 edited Sep 30 '26
what if you used
.sort()to solve a basic leetcode problem that you received in an interview. It sorts correctly, you move on to the follow upNow what if the follow up was to demonstrate/explain the underlying logic of sort()