Skip to content

Commit 0c67a9d

Browse files
Added linearSearch algorithm
1 parent 0a9ac6f commit 0c67a9d

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Search/LinearSearch/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#Run the linearSearch.js as
2+
3+
`node fileName inputArray key`
4+
5+
example:
6+
7+
`node linearSearch.js [1,2,3,4] 3`
8+
9+
Output:
10+
11+
1. Success - `Key found at index: index`
12+
2. Failure - `Key Not Found`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Program implementing Linear Search
2+
3+
let args = process.argv;
4+
5+
args[2] = args[2].replace("[", "");
6+
args[2] = args[2].replace("]", "");
7+
8+
let arr = args[2].split(",").map(Number);
9+
key = Number(args[3]);
10+
let keyFound = false;
11+
let index = 0;
12+
for (let i = 0; i < arr.length; i++) {
13+
if (Number(arr[i]) === key) {
14+
keyFound = true;
15+
index = i;
16+
}
17+
}
18+
19+
if (keyFound === true) {
20+
console.log(`Key found at index: ${index}`);
21+
} else {
22+
console.log("Key Not Found");
23+
}

0 commit comments

Comments
 (0)