This doesn't iterate again over the colors encountered, and uses the minimal memory required for the task: The running total of pairs and the unpaired colors, encountering which again would be the completion of a pair.
Hello Mihail and thank you for both of your detailed inputs. I have a final episode and webinar coming up to allow us in taking a detail look at why one solution over the other. If you go the Hacker Rank site you will in fact find numbers of way that this one challenge is solved. So it will be pretentious from me to say that this one is the best one of all the possible ones. But when it comes to the first solution provided in episode 1 and this one, this is by far a better solution as far as approach and performance.
I have added the two functions you have provided and run all three against a simple benchmark to see how each performs against a set of 10,000 items. Check the screenshot below:
// First solution episode 1$nodesockMerchant.js4997SortAndCount:74.724ms// This solution episode 2$nodesockMerchant.js4997StockAndCount:16.788ms// Your first solution$nodesockMerchant.js4997StockAndCount2:49.022ms// Your second solution$nodesockMerchant.js4997StockAndCount3:77.768ms
I have call and run each of those separately running the setup below:
I really like the points you raised regarding side effects and type coercion. I will make sure that those are addressed during the webinar.
It will be great if you could join and offer some insights.
My own solution would be:
This doesn't iterate again over the colors encountered, and uses the minimal memory required for the task: The running total of pairs and the unpaired colors, encountering which again would be the completion of a pair.
Hello Mihail and thank you for both of your detailed inputs. I have a final episode and webinar coming up to allow us in taking a detail look at why one solution over the other. If you go the Hacker Rank site you will in fact find numbers of way that this one challenge is solved. So it will be pretentious from me to say that this one is the best one of all the possible ones. But when it comes to the first solution provided in episode 1 and this one, this is by far a better solution as far as approach and performance.
I have added the two functions you have provided and run all three against a simple benchmark to see how each performs against a set of 10,000 items. Check the screenshot below:
I have call and run each of those separately running the setup below:
I really like the points you raised regarding side effects and type coercion. I will make sure that those are addressed during the webinar.
It will be great if you could join and offer some insights.
Cheers :)
Yup, my "highly scientific" browser benchmark also makes it seem that
Set
is slower: