You are viewing a single comment's thread. Return to all comments →
This isn't an issue with boiler plate. Your code is failing for cases where there is large amount of queries for op 3 that don't find a matching value.
containsValue is probably O(n) since the value list is not sorted.
Imagine your hash had >100k unique entries all with a frequency of one. Each query with op 3 would have to do 100k tests if the the value was two. This would be true even if every call to op 3 was testing for the same exact value each time!
That is going to be really really slow. Especially if you do all op 3 queries after all op 1 and op 2 queries. The hash table is not changing and we're passing the same values over and over and yet this code does O(n) look up every time.
If you want it to be fast you need a reverse look up of frequency value to list of operand.
This is what helped me.
Ended up having to add some extra operations to op 1 and 2 to maintain a second map of frequencies. But op 3 is now O(1), which was the bottleneck.