Linear search in JavaScript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Linear search in JavaScript

Although we do linear search using if else statement and indexOf(); very easily without using any loop .... Like this https://www.sololearn.com/post/904561/?ref=app Then why all tutorials teach to use loop for linear search ?

27th Jan 2021, 3:30 PM
Prasant
Prasant - avatar
13 Answers
+ 3
โ„๐•’๐•ฃ๐•ฃ๐•ช ๐Ÿ’• ๐•ป๐–”๐–™๐–™๐–Š๐–— โšกโœจ For your Non loop code above check this for an optimization where the indexOf() function is ran once. var arr = [5, 7, 2, 9, 0, 7]; function search(k){ i = arr.indexOf(k); if (i == -1) return i; return ++i; } console.log(search(7)); For your loop attempt if the if condition is false it returns -1. The very 1st check is false and no further checks are made. Loop through the array and only do an if statement, no else, to return the index if found. Then after the loop return -1 to cover a not found. function search(array, k){ for(let i = 0; i < array.length; i++){ if(array[i] === k) return i+1; } return -1; }
27th Jan 2021, 4:28 PM
ChaoticDawg
ChaoticDawg - avatar
+ 8
Perhaps so that we can understand better what's going on behind the scene? Many search algorithms implements loops behind the scene. The difference is, to either use built-in search feature provided, or to try implement it on our own, in which case we understand how a search algorithm works internally. Although, in high level languages, use of built-in features is encouraged more over self implementation, in consideration of code reuse, and development period reduction. Personally I see self implementation of search algorithm is more closely related with academic purposes. Not saying, it's bad, just that I have and am still going through constant surprise times finding out that what I learn to do apparently has been available, just my lack of knowledge of its presence : )
27th Jan 2021, 3:48 PM
Ipang
+ 4
That code isn't an actual linear search, but how do you think indexOf() works? Try writing the code for indexOf() yourself and then you'll write a real linear search. FYI, that code could and should be optimized further by only calling indexOf() once instead of "searching" the array for the index 3 times which increases the runtime. https://en.m.wikipedia.org/wiki/Linear_search#:~:text=In%20computer%20science%2C%20a%20linear,whole%20list%20has%20been%20searched.
27th Jan 2021, 3:41 PM
ChaoticDawg
ChaoticDawg - avatar
+ 4
โ„๐•’๐•ฃ๐•ฃ๐•ช ๐Ÿ’• ๐•ป๐–”๐–™๐–™๐–Š๐–— โšกโœจ I'm not an expert of algorithms, so I am not in position to say. But for general cases, and in relation to high level languages, use of built-in search feature is always encouraged.
27th Jan 2021, 3:58 PM
Ipang
+ 3
Ipang Yes, by using loops, we go deep into the algorithm to understand better.. But can I use this built-in indexOf(); for linear search in every case ?
27th Jan 2021, 3:53 PM
Prasant
Prasant - avatar
+ 3
Got it now ! indexOf() and if else can be used for linear search without loop, if no element is repeated It will show only first index of repeated element like this https://code.sololearn.com/WzqxI7Eql0ck/?ref=app we have to use loop, if the elements are repeating Am I right ? ChaoticDawg sir Ipang sir Kode Krasher sir
27th Jan 2021, 4:05 PM
Prasant
Prasant - avatar
+ 3
ChaoticDawg I understood sir https://code.sololearn.com/WD8fOpFVQS7D/?ref=app I tried, but it's showing -1 instead of actual index (5) Sir, can you please tell me where is the error ?
27th Jan 2021, 4:15 PM
Prasant
Prasant - avatar
27th Jan 2021, 4:19 PM
Ipang
+ 3
ChaoticDawg sir, I understood the loop attempt. https://code.sololearn.com/WzqxI7Eql0ck/?ref=app But couldn't understand the non-loop attempt
27th Jan 2021, 4:55 PM
Prasant
Prasant - avatar
+ 2
ChaoticDawg But it's showing correct answer in geeks for geeks https://www.sololearn.com/post/904561/?ref=app Look at the bottom of the image
27th Jan 2021, 3:44 PM
Prasant
Prasant - avatar
+ 2
Just because the outputs are correct doesn't mean you accomplished the given task as intended
27th Jan 2021, 3:45 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
โ„๐•’๐•ฃ๐•ฃ๐•ช ๐Ÿ’• ๐•ป๐–”๐–™๐–™๐–Š๐–— โšกโœจ When it comes to Javascript and performing a linear search on an array, yes, you should use the built-in indexOf(). However, with custom built data structures this may not work, so you may need to implement your own search for your custom structures etc. This is why you also need the knowledge of how to do so and how such functions are working behind the scenes. Built-in functions are typically already optimized so they should be used when possible in your real world code. The intent for lessons such as the one you're doing is to expand your knowledge base and problem solving abilities by writing the code yourself. This is what I was attempting to hint at.
27th Jan 2021, 4:09 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
โ„๐•’๐•ฃ๐•ฃ๐•ช ๐Ÿ’• ๐•ป๐–”๐–™๐–™๐–Š๐–— โšกโœจ It only makes 1 call to the indexOf() function, which is basically a linear search function itself. This saves on computational runtime. The indexOf() function will return -1 if the element is not found in the search, so we use an if statement to check if the returned value is -1 an return it if so. Otherwise the index + 1 is returned.
27th Jan 2021, 5:04 PM
ChaoticDawg
ChaoticDawg - avatar