JavaScript Algorithms - 15 - Linear Search

preview_player
Показать описание

📱 Follow Codevolution

Linear Search
JavaScript Algorithms
Algorithms with JavaScript
Рекомендации по теме
Комментарии
Автор

finally a question i solved before watching the whole video😊😊

saifhamdare
Автор

Thank you so much bro. You are always there to save my programming life. Pls do consider redux-persist with rtk, next redux wrapper too. Gratias 👍

ifeanyialex
Автор

Using arr.indexOf(target) return index of the element if present or -1 if not present.
Time complexity - O(1)

bhavanikunapuli
Автор

Awesome bro 👍 your explanation is too good.

anandr
Автор

You should have increase the font size or zoom in chrome tab

rohankatkam
Автор

Thanks for your videos, it is very helpful.

anandphani
Автор

If an interviewer ask me to optimize the code, is it a good solution to just use array.indexOf(target)?

adrymateoramon
Автор

is the code he provided not the same as doing indexOf(), since he's iterating the length of the array anyways? In terms of time complexity.

PROTOTYPZ
Автор

Hello,
Why didn't you choose forEach here ?
I have used below code can you let me know is it O(n) or not?

function lSearch(arr, tar){
let retVal;
arr.forEach((elm, i) => {
if(elm == tar){
retVal = i;
}
});
return retVal || -1;
}


I have doubt bcz this forEach also looks small compared to for loop.
Please let me know if I am getting something wrong.

binayakbidyasagar
Автор

Hi Vishwas,

One doubt

function linearSearch(arr = [], ele) {
arr.forEach((item, index) => {
if (item === ele) return index;
});
return -1;
}

console.log(linearSearch([-5, 1, 10, 11, 4], 10))

the output is coming as -1. Can you please explain why is it so?

susmitobhattacharyya
Автор

I don't know how to trace function execution with pen and paper, can anyone explain what that mean?

Ea_fc