1. 선형 검색

문제: n개의 요소로 구성된 배열 arr[]이 주어지면 arr[]에서 주어진 요소 x를 검색하는 함수를 작성하십시오.
 
notion image
// Javascript code to linearly search x in arr[]. If x // is present then return its location, otherwise // return -1 function search(arr, n, x) { let i; for (i = 0; i < n; i++) if (arr[i] == x) return i; return -1; } // Driver code let arr = [ 2, 3, 4, 10, 40 ]; let x = 10; let n = arr.length; // Function call let result = search(arr, n, x); (result == -1) ? document.write("Element is not present in array") : document.write("Element is present at index " + result); // This code is contributed by Manoj