Welcome to the repository for solving a common coding interview problem: Finding duplicates in an array using Java! π
This repository contains a simple yet efficient solution to identify duplicate elements in an array, explained step-by-step with clean, optimized code.
Given an array of integers, find all elements that occur more than once. If no duplicates exist, return an empty list.
Input: [4, 3, 2, 7, 8, 2, 3, 1]
Output: [2, 3]
This solution leverages:
- HashSet: To track seen elements efficiently.
- ArrayList: To store duplicate elements.
The algorithm is efficient, with a time complexity of O(n) and space complexity of O(n).
Hereβs the implementation:
import java.util.*;
class Solution {
public List<Integer> findDuplicates(int[] arr) {
List<Integer> result = new ArrayList<>();
Set<Integer> seen = new HashSet<>();
for (int num : arr) {
if (!seen.add(num)) {
result.add(num);
}
}
return result;
}
}
- Clone this repository to your local machine:
git clone https://github.com/yourusername/find-duplicates-in-array-java.git
cd find-duplicates-in-array-java
- Compile the code:
javac Solution.java
- Run the program with an example input:
java Solution
For a detailed, step-by-step walkthrough of the code, watch my YouTube video: π Java Coding Tutorial: Find Duplicates in an Array - https://youtu.be/ukuKhemNH3g
If you have any improvements or suggestions, feel free to fork the repo and create a pull request. Contributions are always welcome! π
If you have questions or want to collaborate, feel free to connect with me: π§ Email: rahulvijayan2291@gmail.com π LinkedIn: https://www.linkedin.com/in/rahul-vijayan-682a12194/ π Donβt forget to star this repository if you found it helpful!
This project is licensed under the MIT License - see the LICENSE file for details.