Skip to main content

How to search for an element in a LinkedList in Kotlin

How to search for an element in a LinkedList in Kotlin.

Here is a detailed step-by-step tutorial on how to search for an element in a LinkedList in Kotlin.

Step 1: Create a LinkedList

To begin with, we need to create a LinkedList in Kotlin. A LinkedList is a data structure that consists of a sequence of elements, where each element has a reference to the next element in the sequence.

val linkedList = LinkedList<String>()

Step 2: Add elements to the LinkedList

Next, we need to add some elements to the LinkedList. We can use the add method to add elements at the end of the LinkedList.

linkedList.add("Apple")
linkedList.add("Banana")
linkedList.add("Orange")

Step 3: Search for an element in the LinkedList

To search for an element in the LinkedList, we can iterate through each element and compare it with the element we are searching for. If a match is found, we can return the index or perform any other desired action.

fun searchElement(linkedList: LinkedList<String>, element: String): Int {
for (index in 0 until linkedList.size) {
if (linkedList[index] == element) {
return index
}
}
return -1 // Element not found
}

In the above code, we define a function searchElement that takes a LinkedList and an element to search for as parameters. It iterates through each element in the LinkedList and compares it with the search element. If a match is found, it returns the index of the element. If the element is not found, it returns -1.

Step 4: Test the search functionality

Now, let's test the search functionality by calling the searchElement function and passing the created LinkedList and the element we want to search for.

val index = searchElement(linkedList, "Banana")
if (index != -1) {
println("Element found at index: $index")
} else {
println("Element not found")
}

In the above code, we call the searchElement function and pass the LinkedList and the element "Banana". If the element is found, it prints the index at which the element is located. Otherwise, it prints "Element not found".

Step 5: Handle multiple occurrences of the element

If the LinkedList contains multiple occurrences of the search element, the searchElement function will return the index of the first occurrence. If you want to find all occurrences of the element, you can modify the function to store the indices in a list and return that list.

fun searchElement(linkedList: LinkedList<String>, element: String): List<Int> {
val indices = mutableListOf<Int>()
for (index in 0 until linkedList.size) {
if (linkedList[index] == element) {
indices.add(index)
}
}
return indices
}

In the above code, we modify the searchElement function to return a list of indices instead of a single index. We create an empty list indices to store the indices of all occurrences. Whenever a match is found, we add the index to the list. Finally, we return the list of indices.

Now you have learned how to search for an element in a LinkedList in Kotlin. You can use this knowledge to implement search functionality in your Kotlin applications.