Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Write a function to find duplicate elements in an array.

Hard

Problem Statement

Write a function to find duplicate elements in an array.

Sample Input

A hardcoded array of integers, e.g., [1, 2, 3, 2, 4, 5, 3].

Sample Output

Duplicate elements: [2, 3]

Limits

Time: 2s · Memory: 256 MB

Code Editor
00:00
fun findDuplicates(nums: Array<Int>): List<Int> { val seen = mutableSetOf<Int>() val duplicates = mutableSetOf<Int>() for (num in nums) { if (!seen.add(num)) { duplicates.add(num) } } return duplicates.toList() } fun main() { val nums = arrayOf(1, 2, 3, 2, 4, 5, 3) println("Duplicate elements: ${findDuplicates(nums)}") }
Log in or sign up to run or submit your code.Log inSign up
Input
A hardcoded array of integers, e.g., [1, 2, 3, 2, 4, 5, 3].
Expected Output
Duplicate elements: [2, 3]