Challenge: Find Duplicates in Array

Write a function to find the duplicates in an array in the most efficient way.

Problem statement

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

Input

An integer array.

Output

The output is an ArrayList containing the duplicates if they exist and an empty ArrayList if they don’t.

Sample input

  int arr[] = {1, 3, 4, 3, 5, 4, 100, 100};

Sample output

  ArrayList <Integer> output = {3, 4, 100};

Coding exercise

Take a close look 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 code tab. Good Luck!

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