Last active
November 1, 2020 04:01
-
-
Save stephengfriend/0d7a4d6fe5b94a578f957af544e7ebea to your computer and use it in GitHub Desktop.
Simple Search Solutions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Sample code to perform I/O: | |
| process.stdin.resume(); | |
| process.stdin.setEncoding("utf-8"); | |
| var stdin_input = ""; | |
| process.stdin.on("data", function (input) { | |
| stdin_input += input; // Reading input from STDIN | |
| }); | |
| process.stdin.on("end", function () { | |
| main(stdin_input); | |
| }); | |
| function main(input) { | |
| lines = input.split('\n') // Array of input lines | |
| const N = lines[0]; // Count of values | |
| const values = lines[1].split(' '); // Array of values | |
| const K = lines[2]; // The value to search for | |
| // For loop solution | |
| for (let index = 0; index < values.length; index++) { | |
| if (values[index] === K) { | |
| console.log(index); | |
| break; | |
| } | |
| } | |
| // While loop solution | |
| let index = 0; | |
| while (index < values.length) { | |
| if (values[index] === K) { | |
| console.log(index); | |
| break; | |
| } | |
| index++; | |
| } | |
| // Built-in solution | |
| console.log(values.indexOf(lines[2])); | |
| // Least amount of lines | |
| console.log(lines[1].split(' ').indexOf(lines[2])); | |
| } |
Author
Thanks man
Line 40 -44 are they from the question itself?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solutions for https://www.hackerearth.com/practice/algorithms/searching/linear-search/practice-problems/algorithm/simple-search-4/description/