Find Target Index
JavaScript
Easy
4 views
Problem Description
Given an array and a value x, find the first index (0-based) where x is present. If not present, print -1.
Input Format
Line1: n x. Line2: n integers.
Output Format
One integer index.
Official Solution
const fs=require('fs');const raw=fs.readFileSync(0,'utf8').trim();if(!raw)process.exit(0);const a=raw.split(/\\s+/);let i=0;const n=Number(a[i++]);const x=a[i++];let ans=-1;for(let j=0;j<n;j++){const v=a[i++];if(ans===-1 && v===x)ans=j;}process.stdout.write(String(ans));
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!