Recently I decided to enhance my programming logic so I started making some leetcode problems daily just to exercise, but as always I feel a lot of difficulty doing this logic exercises until youtube recommended this video from a Brazilian tech content creator where he teach how to solve one of the most common exercises from tech interviews using a map.
what is a map
Map is a data structure where can store a value combined with a key, so if you need to access this data later you can use the key instead of interating an entire array to find a value (just like the filter and find methods in javascript).
But why should I use a map instead of array?
You can simplify the logic with a map instead of using a lot of for loops to make the exercise, being more legible and understandable. When you iterate an array, your code have the complexity of O(n) where n is the size of the array because you have to iterate the entire array, when using a Map, operations such as get and set have a time complexity of O(1) because the Map relies on a hash table internally. This structure allows the engine to compute the exact location of a value based on its key, so it can access or update entries directly without iterating through all keys and values. As a result, the performance of these operations remains constant regardless of the size of the Map, depending only on the key being used rather than the total number of elements stored.
Top comments (0)