Linear Search in Kotlin

Published on 13 February 2025 (Updated: 13 February 2025)

Welcome to the Linear Search in Kotlin page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

fun main(args: Array<String>)  
{
    // store usage message in variable
    val message = "Usage: please provide a list of integers (\"1, 4, 5, 11, 12\") and the integer to find (\"11\")"
       
    // validate input array is correct size and does not contain empty Strings 
    if(args.size !=2 || args[0].isBlank() || args[1].isBlank())
    {
        println(message)
        return
    } 
    
    // convert input number String into a List of integers, invalid characters are converted to null
    val intList = args[0].split(",").map { it.trim().toIntOrNull() }
 
    // convert input key String into an int, invalid characters are converted to null
    val key = args[1].toIntOrNull()
 
    // check if the List or the key contains null (invalid) elements
    if(null in intList || key == null)
    {
        println(message)
        return
    }

    // check if key is in the List and print returned boolean   
    println(key in intList)
}

Linear Search in Kotlin was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.