in leetcode for majority elements II question, i used the hashmap to keep track of how many times each element has been repeated but in geeks for geeks i saw them use an array of structure, so i wanted to know which was faster and takes up more space!
Asked
Active
Viewed 219 times
-3
-
2Arrays are generally much more compact. They are also much faster when the number of item is small. When the amount of data is big, the performance is strongly dependent of your algorithm. – Jérôme Richard Apr 16 '21 at 10:10
1 Answers
1
Objects take additional space as hashmap requires three extra objects or each entry whereas array doesn't. So, Hashmap takes more space as compared to array; however, in case of hashmap, one gets results much faster that array. Therefore, it depends on the problem constraints; whether to use hashmap or array

Kuljit Kaur
- 11
- 2