Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
I really like this idea of filtering out the negative items first!
Neat! Very clean.
Unrelated: reading your solution made Big O notation make sense to me. :)
Every item in the incoming array is processed twice, first to remove everything that's equal to or greater than zero to a fresh array, and then to sum every element of that new list. Every item is essentially touched twice so it's O squared, but technically maybe it's kind of smaller than that in an ideal case because the reducer only does its work on the resulting, probably smaller, subset of filtered numbers.
Thanks, I know this is sort of unrelated, I just needed to go off for a sec.
for initialitation
valeur a will begin with 0
like my solution hhhhh
best way to solve this, small and conscise!!!
Thanks a lot
It's the parameter
initialValue
ofArray.prototype.reduce
. You can read more about it here.Could you please answer my question? Why do you have to put '0' at the end of the expression?
This is the better solution for me. It's clear and fast.Congratulations!
Absolutely genious.
You should be correct, this is O(2n). This solution should be perfectly acceptable and scale just fine with large lists. There is no need to optimize unless the code is causing actual performance issues. Readability and intent should be king unless you are seeing some issues with the code. Even running this with 6x CPU throttling, the duration for filter-then-reduce is 0.6ms for me on an array with 10000 elements. (Base CPU Ryzen 5 3600X, throttled 6x using Chrome dev tools) Unless this is being run extremely frequently or on extraordinarily large lists, it should not be a noticeable problem.
Wouldn't it actually be O(2n)? n for filter + n for reduce? Or am I misunderstanding something?
An improvement from O(2n) to O(n) is not that significant in most cases.
Voile, you must've mistunderstood. This is about
x>=0
, not the, 0
.Personally i wouldn't take this approach..Your solution is O(n^2).For large inputs of n this is not recommendable