Skip to content

Instantly share code, notes, and snippets.

@stephengfriend
Last active November 1, 2020 04:01
Show Gist options
  • Select an option

  • Save stephengfriend/0d7a4d6fe5b94a578f957af544e7ebea to your computer and use it in GitHub Desktop.

Select an option

Save stephengfriend/0d7a4d6fe5b94a578f957af544e7ebea to your computer and use it in GitHub Desktop.
Simple Search Solutions
// 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]));
}
@stephengfriend
Copy link
Author

stephengfriend commented Oct 30, 2020

@Cool-sami12
Copy link

Thanks man

@Cool-sami12
Copy link

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