Explanation

576.Linear Search for a Value

Easy
Given an array nums of distinct values and a target, return the index where the target appears, or -1 if it is not in the array. Linear searchLinear SearchChecking elements one by one from the start until the target is found or the array ends. Works on any array, sorted or not. is the most basic search there is: walk through the array in order and compare. It needs no preparation and works on unsorted data, at the price of up to n comparisons. Returning a sentinel valueSentinel ValueA special return value, like −1, that cannot be a real answer and therefore signals "no result". like −1 is the standard way to report "not found".

Example 1:

Input: nums = [14,3,27,8], target = 27

Output: 2

Example 2:

Input: nums = [14,3,27,8], target = 5

Output: -1

+ 4 hidden test cases run on Submit.

Constraints:

  • ●1 ≤ nums.length ≤ 1000
  • ●all values in nums are distinct
  • ●-1000000 ≤ nums[i], target ≤ 1000000

nums =

[14, 3, 27, 8]

target =

27