easyArraysPattern: Linear Search

Find Target Element Solution

Problem Statement

You are given an array of integers `nums` and a target integer `target`. Implement a linear search algorithm to find the index of the `target` element in the `nums` array. If the `target` element is not found, return -1.

Examples

Example 1:
Input:[23, 817, 46, 279, 98, 117], 817
Output:1
Explanation: The index of the desired beacon identifier 817 in the array is 1 (0-indexed)
Example 2:
Input:[23, 817, 46, 279, 98, 117], 46
Output:2
Explanation: The index of the desired beacon identifier 46 in the array is 2 (0-indexed)

Constraints

  • Scores are integers
  • Student IDs are unique
  • Scores have varying maximum values
Time: O(n) Space: O(1)
The optimized approach is to use a linear search algorithm with a time complexity of O(n)

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.