Skip to content

Commit 0da0676

Browse files
Merge pull request matthewsamuel95#527 from shakib609/master
Add LinearSearch implementation in Javascript
2 parents 6cf2bac + 77b1868 commit 0da0676

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Search/LinearSearch/linear_search.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// The function takes an array and the element as input
2+
// Returns the position if the element is found
3+
// Otherwise, returns -1
4+
function linear_search(array, element) {
5+
const n = array.length;
6+
for (i = 0; i < n; i++) {
7+
if (array[i] == element)
8+
return i;
9+
}
10+
return -1;
11+
}
12+
13+
14+
const arr = [4, 12, 7, 90, 88, 14];
15+
const element = 90;
16+
const position = linear_search(arr, element);
17+
18+
if (position == -1)
19+
console.log("Element not found!");
20+
else
21+
console.log("Element found at: " + position);

0 commit comments

Comments
 (0)