Kotlin Program to find duplicate elements in the array


In this program, You will learn how to find duplicate elements in the array in Kotlin.


List is: 2 3 4 5 2 3 4 5 9 10

Duplicate elements is: 2 3 4 5

Example: How to find duplicate elements in the array in Kotlin.

import java.util.Scanner

fun main(args: Array<String>) {

    var v = 1
    val arr = IntArray(5)
    val sc = Scanner(System.`in`)

    print("Enter 5 elements:")

    for (i in 0 until arr.size) {
        arr[i] = sc.nextInt()
    }

    print("Duplicate element list are:")

    for (i in 0 until arr.size) {
        for (j in i + 1 until arr.size) {
            if (arr[i] == arr[j]) {
                if (v == 1 && arr[j] != 0) {
                    print("${arr[i]} ")
                }
                arr[j] = 0
                v++
            }
        }
        v = 1
    }
}

Output:

Enter 5 elements:10 20 10 20 30
Duplicate element list are:10 20