DEV Community

Discussion on: A classic interview question

Collapse
 
candidateplanet profile image
lusen / they / them πŸ³οΈβ€πŸŒˆπŸ₯‘

or stuff each string into a hashmap (python duct, js object) that maps each letter to a count of occurrences. then compare values and keys. both are linear operations, thus more optimal than sorting.

maps tend to be the more optimal solution to β€œfinding things in arrays using nested loops” than sorting.

sorting is more useful when you have space constraints β€” eg you can sort in place and then compare without hashmaps β€” or want to do additional work than exact comparison β€” eg navigate a tree representation of words in incremental ways.