Kotlin Program for Linear Search with Example
A Kotlin program for linear search uses a
for loop to check each element in an array and returns the index if the target is found, like for (i in arr.indices) { if (arr[i] == target) return i }.Examples
Inputarr = [3, 5, 7, 9], target = 7
Output2
Inputarr = [10, 20, 30, 40], target = 25
Output-1
Inputarr = [], target = 1
Output-1
How to Think About It
To find a number in a list, start from the first item and check each one in order. If the item matches the number you want, stop and return its position. If you reach the end without finding it, say it is not there.
Algorithm
1
Get the array and the target value.2
Start from the first element and check each element one by one.3
If the current element equals the target, return its index.4
If no element matches after checking all, return -1.Code
kotlin
fun linearSearch(arr: IntArray, target: Int): Int {
for (i in arr.indices) {
if (arr[i] == target) {
return i
}
}
return -1
}
fun main() {
val numbers = intArrayOf(3, 5, 7, 9)
val target = 7
val result = linearSearch(numbers, target)
println(result)
}Output
2
Dry Run
Let's trace the example where arr = [3, 5, 7, 9] and target = 7 through the code.
1
Start loop
i = 0, arr[0] = 3, target = 7
2
Check first element
3 == 7? No, continue
3
Next element
i = 1, arr[1] = 5
4
Check second element
5 == 7? No, continue
5
Next element
i = 2, arr[2] = 7
6
Check third element
7 == 7? Yes, return index 2
| Index (i) | Array Value | Target | Match? |
|---|---|---|---|
| 0 | 3 | 7 | No |
| 1 | 5 | 7 | No |
| 2 | 7 | 7 | Yes |
Why This Works
Step 1: Loop through array
The for loop goes through each element one by one to check if it matches the target.
Step 2: Compare elements
Each element is compared with the target using == to find a match.
Step 3: Return index or -1
If a match is found, the index is returned immediately; otherwise, -1 signals the target is not in the array.
Alternative Approaches
Using while loop
kotlin
fun linearSearchWhile(arr: IntArray, target: Int): Int {
var i = 0
while (i < arr.size) {
if (arr[i] == target) return i
i++
}
return -1
}
fun main() {
val numbers = intArrayOf(3, 5, 7, 9)
println(linearSearchWhile(numbers, 7))
}This uses a <code>while</code> loop instead of <code>for</code>, which is more manual but works the same.
Using built-in indexOf
kotlin
fun main() {
val numbers = intArrayOf(3, 5, 7, 9)
val target = 7
val index = numbers.indexOf(target)
println(index)
}Kotlin's <code>indexOf</code> function does linear search internally and is simpler to use but less educational.
Complexity: O(n) time, O(1) space
Time Complexity
The program checks each element once in the worst case, so time grows linearly with array size.
Space Complexity
No extra space is used except a few variables, so space is constant.
Which Approach is Fastest?
All linear search methods have the same time complexity; using built-in indexOf is concise but similar in speed.
| Approach | Time | Space | Best For |
|---|---|---|---|
| For loop linear search | O(n) | O(1) | Learning and manual control |
| While loop linear search | O(n) | O(1) | Manual iteration style |
| Built-in indexOf | O(n) | O(1) | Quick and simple code |
Use Kotlin's
indexOf for quick linear search in real projects.Forgetting to return
-1 when the target is not found causes wrong results.