## 1. Introduction

In Kotlin, a List is a collection of elements in a specific order. Sometimes, we might need to find out how many times a particular element appears in the list. This can prove useful in situations such as identifying duplicates or counting the frequency of specific list elements.

In this tutorial, we’ll explore various ways to accomplish this task in Kotlin.

## 2. Using a for() Loop

A straightforward way to find the number of times a particular value appears in a list is by using the classic for() loop. We iterate over the elements of the list and keep track of the number of occurrences of that value in the list:

``````fun findRepeatedValuesUsingForLoop(value: Int, list: List<Int>): Int {
var count = 0
for (i in 0 until list.size - 1) {
if (list[i] == value) {
count ++
}
}

return count
}``````

Our method accepts two parameters: the value we’re checking for, and the list of values. It returns the number of times the value appears in the list.

First, we initialize a counter with zero. Then, we iterate the list elements and increment the counter for each occurrence of that value. Finally, we return its count as a result.

Now, let’s make sure this method runs as expected:

``````@Test
fun `find repeated elements in a list using for loop`() {
val list = listOf(1, 2, 3, 2, 4, 3, 5, 2, 6)

assertEquals(3, findRepeatedValuesUsingForLoop(2, list))
assertEquals(2, findRepeatedValuesUsingForLoop(3, list))
assertEquals(1, findRepeatedValuesUsingForLoop(4, list))
assertEquals(0, findRepeatedValuesUsingForLoop(9, list))
}``````

This test correctly found the number of occurrences for several elements, and it also correctly recognized when an element did not exist in the list.

## 3. Using the count() Method

Alternatively, we can use the count() method in Kotlin to achieve the same result. This method accepts a lambda function as an argument and returns the number of elements in the list that satisfy the condition specified by the lambda expression:

``````@Test
fun `find repeated elements in a list using count method`() {
val list = listOf(1, 2, 3, 2, 4, 3, 5, 2, 6)

assertEquals(3, list.count{it == 2})
assertEquals(2, list.count{it == 3})
assertEquals(1, list.count{it == 4})
assertEquals(0, list.count{it == 9})
}``````

In each assertion, we begin by calling the count() method on the list. Then, we pass in a lambda function that checks if each element equals the value we’re looking for. As a result, the method returns the number of elements in the list that match the predicate.

## 4. Using a HashMap

Another viable approach that we can explore involves the use of a HashMap. To be more precise, we’ll construct a HashMap using the values in the list as keys and the number of occurrences as values:

``````fun findRepeatedValuesUsingHashMap(value: Int, list: List<Int>): Int {
val map = HashMap<Int, Int>()
for (element in list) {
map[element] = map.getOrDefault(element, 0) + 1
}
return if(!list.contains(value)) 0 else map.getValue(value)
}``````

We begin by creating an empty HashMap. Then, as we iterate over the elements of the list, we update the counts in the map. To achieve this, we use the getOrDefault() method, which returns the current count of an element or zero if it’s not present in the map.

Once we’ve grouped all the elements into a frequency map, we return the number of occurrences of the value we want to count from the map. If the value doesn’t exist, we return zero.

As usual, we test our method for correctness:

``````@Test
fun `find repeated elements in a list using hashmap`() {
val list = listOf(1, 2, 3, 2, 4, 3, 5, 2, 6)

assertEquals(3, findRepeatedValuesUsingHashMap(2, list))
assertEquals(2, findRepeatedValuesUsingHashMap(3, list))
assertEquals(1, findRepeatedValuesUsingHashMap(4, list))
assertEquals(0, findRepeatedValuesUsingHashMap(9, list))
}``````

## 5. Conclusion

In this article, we’ve explored various ways to find repeated elements in a list. The first approach is much simpler as it requires no built-in methods or data structures. However, the HashMap and count() methods leverage data structures and built-in methods, respectively. Nevertheless, we encourage developers to adopt any of these approaches that appeal to their needs.