fork download
  1. // BAD ❌ O (n)
  2. function linearSearch(arr, value) {
  3. for (let i = 0; i < arr.length; i++) {
  4. if (arr[i] === value) return i;
  5. }
  6. return -1;
  7. }
  8. const sortedArray = [1, 3, 5, 7, 9, 11, 13];
  9. console.log(linearSearch(sortedArray, 7)); // Output: 3
Success #stdin #stdout 0.04s 16568KB
stdin
Standard input is empty
stdout
3