That's a great solution 👍. However, as we see here, indexOf has a complexity of O(N). This means that for every stone, we might have to check the whole jewels string rather than a data structure we've constructed.
For example, say we are passed 100 stones and 1000 jewels. With indexOf, the rough number of operations is stones * jewels, or 100 * 1000. What we really want is stones * Set#has, or 100 * 1. Set#has cost.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
What about this solution for jewels problem?
function findTotal(J, S) {
return Array.from(S).filter(item => J.indexOf(item) !== -1).length;
}
That's a great solution 👍. However, as we see here,
indexOf
has a complexity ofO(N)
. This means that for every stone, we might have to check the whole jewels string rather than a data structure we've constructed.For example, say we are passed 100 stones and 1000 jewels. With
indexOf
, the rough number of operations isstones * jewels
, or100 * 1000
. What we really want isstones * Set#has
, or100 * 1
. Set#has cost.