Find If A Value Exists In Array - JavaScript

preview_player
Показать описание
Here are a few ways to find if a value in present within an array, useful for coding interviews at companies like Microsoft, Amazon, Apple and Google

#softwareengineering #javascript #codinginterview #shorts
Рекомендации по теме
Комментарии
Автор

Also with the set you have a time complexity of O(n). You habe to create the set in O(n) time and then you perform the lookup in O(1), so it does not really make sense to create a set if you do not have to look up multiple values 🤷‍♂️

mikeeG
Автор

Variant 2 is the best because it's most human readable. The code tells you exactly what it does.

ForgottenKnight
Автор

So I did a microtest (not perfect but give an idea) to verify your claims.

I created an array with a million item and use both the includes and set method.

Set: 87ms (consistantly above 80ms)
Includes: 5ms (consistantly under 10ms)

This is not margin of error so... wrong, not O(1). Before affirming something, at least try to do a micro-benchmark to double check

elvarght
Автор

Great way to learn things quickly, please keep up ❤

sahilambekar
Автор

So what if we have an array of objects do we need to loop it?

Volatile
Автор

indexOf or includes are jokes for you?

biyrololo
Автор

I dunno how fast JS sets are given they maintain ordering

marktyree
Автор

Sanity check please, it's impossible to check if a value is in an array (regardless of language) in anything faster than O(n), right?

Jia-Tan
welcome to shbcf.ru