Challenge: Find Duplicates in an Array with No Repetition

Solve the challenge of efficiently finding duplicates in an array that initially has no repetitions.

Problem statement

Given an array of integers, find all the duplicates that exist in that array.

Input

An array of duplicate integers.

Note: All the integers are less than the size of the array.

Output

An array with the duplicates if they exist, and an empty array if they don’t.

Sample input

lst = { 1, 3, 1, 3, 5, 1, 4, 7, 7 };

Sample output

result = { 1, 3, 7};

Coding exercise

First, take a close look at this problem and design a step-by-step algorithm before jumping to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the solution provided in the solution section. Good luck!

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.