|
|
|
|
|
by dozzie
3247 days ago
|
|
Ever heard of these things called "hashes" a.k.a. "dictionaries"? They are
magical boxes for elements that are indexed by name. You build such a magical
box of seen elements while going through the checking loop and you get the
time complexity of O(n) or O(n log(n)) instead of your current O(n^2), so it
won't explode when you get more elements passing through the code. Note that this was conceived in just a few minutes of reading your code,
deciphering syntax only remotely familiar, and figuring out what was it
supposed to do, all with cold start (I haven't written much today yet).
Something went wrong with your thought process. |
|
For a final comparison, here's an implementation of the extra loops and manual indexing I suggested:
This isn't significantly longer than the HashSet-based version, but it is even faster to run (I just benchmarked it) and also doesn't use an extra object which takes memory and needs to be managed and garbage-collected later.In conclusion, while I appreciate your suggestion for a possible alternative implementation, I do not appreciate your impugning of my abilities with the suggestion that "something went wrong with your thought process", and implying that I may not be aware of basic data structures.