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

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.