3 Ways To Find Duplicate Elements In An Array - Java - Javarevisited
Maybe your like
Javarevisited
Learn Java, Programming, Spring, Hibernate throw tutorials, examples, and interview questions
Topics and Categories
- core java
- spring
- hibernate
- collections
- multithreading
- design patterns
- interview questions
- coding
- data structure
- OOP
- java 8
- books
- About Me
- Java Certifications
- JDBC
- jsp-servlet
- JSON
- SQL
- Linux
- Courses
- online resources
- jvm-internals
- REST
- Eclipse
- jQuery
- Java IO
- Java XML
Tuesday, July 1, 2025
3 Ways to Find Duplicate Elements in an Array - Java There are multiple ways to find duplicate elements in an array in Java and we will see three of them in this program. The solution and logic shown in this article are generic and apply to an array of any type e.g. String array or integer array or array of any object. One of the most common ways to find duplicates is by using the brute force method, which compares each element of the array to every other element. This solution has the time complexity of O(n^2) and only exists for academic purposes. You shouldn't be using this solution in the real world. The standard way to find duplicate elements from an array is by using the HashSet data structure. If you remember, Set abstract data type doesn't allow duplicates. You can take advantage of this property to filter duplicate elements. This solution has a time complexity of O(n), as you only need to iterate over array once, but also has a space complexity of O(n) as you need to store unique elements in the array. Our third solution to find duplicate elements in an array is actually similar to our second solution but instead of using a Set data structure, we will use the hash table data structure. This is a pretty good solution because you can extend it to the found count of duplicates as well. In this solution, we iterate over the array and build the map which stores array elements and their count. Once the table is built, you can iterate over a hash table and print out all the elements, who have a count greater than one, those are your duplicates. Also, basic knowledge of essential data structure is also very important and that's why I suggest all Java programmers join a comprehensive Data Structure and Algorithms course like Data Structures and Algorithms: Deep Dive Using Java on Udemy to fill the gaps in your understanding.How to find duplicates in Java array?
In the first paragraph, I have given you a brief overview of three ways to find duplicate elements from Java array. Now, let's understand the logic behind each of those solutions in little more detail.Solution 1 :
Our first solution is very simple. All we are doing here is to loop over an array and comparing each element to every other element. For doing this, we are using two loops, inner loop, and outer loop. We are also making sure that we are ignoring comparing of elements to itself by checking for i != j before printing duplicates. Since we are comparing every element to every other element, this solution has quadratic time complexity i.e. O(n^2). This solution has the worst complexity in all three solutions. for (int i = 0; i < names.length; i++) { for (int j = i + 1 ; j < names.length; j++) { if (names[i].equals(names[j])) { // got the duplicate element } } } This question is also very popular in programming interviews and if you are preparing for them, I also suggest you solve problems from Cracking the Coding Interview: 150 Programming Questions and Solutions. One of the best books to prepare for software developer interviews.
Solution 2 :
The second solution is even simpler than this. All you need to know is that Set doesn't allow duplicates in Java. Which means if you have added an element into Set and trying to insert duplicate element again, it will not be allowed. In Java, you can use the HashSet class to solve this problem. Just loop over array elements, insert them into HashSet using add() method, and check the return value. If add() returns false it means that element is not allowed in the Set and that is your duplicate. Here is the code sample to do this : for (String name : names) { if (set.add(name) == false) { // your duplicate element } } The complexity of this solution is O(n) because you are only going through the array one time, but it also has a space complexity of O(n) because of the HashSet data structure, which contains your unique elements. So if an array contains 1 million elements, in the worst case you would need a HashSet to store those 1 million elements.Solution 3 :
Our third solution takes advantage of another useful data structure, hash table. All you need to do is loop through the array using enhanced for loop and insert each element and its count into hash table. You can use HashMap class of JDK to solve this problem. It is the general purpose hash table implementation in Java. In order to build table, you check if hash table contains the elements or not, if it is then increment the count otherwise insert element with count 1. Once you have this table ready, you can iterate over hashtable and print all those keys which has values greater than one. These are your duplicate elements. This is in fact a very good solution because you can extend it to found count of duplicates as well. If you remember, I have used this approach to find duplicate characters in String earlier. Here is how you code will look like : // build hash table with count for (String name : names) { Integer count = nameAndCount.get(name); if (count == null) { nameAndCount.put(name, 1); } else { nameAndCount.put(name, ++count); } } // Print duplicate elements from array in Java Set<Entry<String, Integer>> entrySet = nameAndCount.entrySet(); for (Entry<String, Integer> entry : entrySet) { if (entry.getValue() > 1) { System.out.printf("duplicate element '%s' and count '%d' :", entry.getKey(), entry.getValue()); } } Time complexity of this solution is O(2n) because we are iterating over array twice and space complexity is same as previous solution O(n). In worst case you would need a hash table with size of array itself.
Java Program to find duplicate elements in array
Here is our three solutions packed into a Java program to find duplicate elements in array. You can run this example from command line or Eclipse IDE, whatever suits you. Just make sure that name of your Java source file should be same as your public class e.g. "DuplicatesInArray". I have left bit of exercise for you, of course if you would like to do. Can you refactor this code into methods, you can do that easily by using extract method feature of IDE like Eclipse and Netbans and write unit test to check the logic of each approach. This would give you some practice in refactoring code and writing JUnit tests, two important attributes of a professional programmer. packagedto; importjava.util.HashMap; importjava.util.HashSet; importjava.util.Map; importjava.util.Map.Entry; importjava.util.Set; /** * Java Program to find duplicate elements in an array. There are two straight * forward solution of this problem first, brute force way and second by using * HashSet data structure. A third solution, similar to second one is by using * hash table data structure e.g. HashMap to store count of each element and * print element with count 1. * * @author java67 */publicclassDuplicatesInArray{ publicstaticvoidmain(String args[]) { String[] names = { "Java", "JavaScript", "Python", "C", "Ruby", "Java" }; // First solution : finding duplicates using brute force methodSystem.out.println("Finding duplicate elements in array using brute force method"); for (int i =0; i < names.length; i++) { for (int j =i + 1; j < names.length; j++) { if (names[i].equals(names[j]) ) { // got the duplicate element } } } // Second solution : use HashSet data structure to find duplicatesSystem.out.println("Duplicate elements from array using HashSet data structure"); Set<String> store =newHashSet<>(); for (String name : names) { if (store.add(name) ==false) { System.out.println("found a duplicate element in array : "+ name); } } // Third solution : using Hash table data structure to find duplicatesSystem.out.println("Duplicate elements from array using hash table"); Map<String, Integer> nameAndCount =newHashMap<>(); // build hash table with countfor (String name : names) { Integer count = nameAndCount.get(name); if (count ==null) { nameAndCount.put(name, 1); } else { nameAndCount.put(name, ++count); } } // Print duplicate elements from array in JavaSet<Entry<String, Integer>> entrySet = nameAndCount.entrySet(); for (Entry<String, Integer> entry : entrySet) { if (entry.getValue() >1) { System.out.println("Duplicate element from array : "+ entry.getKey()); } } } } Output:Finding duplicate elements in array using brute force method Duplicate elements from array using HashSet data structure found a duplicate element in array :JavaDuplicate elements from array using hash table Duplicate element from array :Java From the output, you can see that the only duplicate element from our String array, which is "Java" has been found by all of our three solutions. That's all about how to find duplicate elements in an array in Java. In this tutorial, you have learned three ways to solve this problem. The brute force way require you to compare each element from array to another, hence has quadratic time complexity. You can optimize performance by using HashSet data structure, which doesn't allow duplicates. So a duplicate element is the one for which add() method of HashSet return false. Our third solution uses hash table data structure to make a table of elements and their count. Once you build that table, iterate over it and print elements whose count is greater than one. This is a very good coding problem and frequently asked in Java Interview. It also shows how use of a right data structure can improve performance of algorithm significantly. If you have like this coding problem, you may also like to solve following coding problems from Java Interviews :- How to find all pairs on integer array whose sum is equal to given number? [solution]
- How to remove duplicates from an array in Java? [solution]
- How to sort an array in place using QuickSort algorithm? [solution]
- Write a program to find top two numbers from an integer array? [solution]
- How do you remove duplicates from array in place? [solution]
- How do you reverse array in place in Java? [solution]
- Write a program to find missing number in integer array of 1 to 100? [solution]
- How to check if array contains a number in Java? [solution]
- How to find maximum and minimum number in unsorted array? [solution]
Recommended books to Prepare for Software Engineer Interviews
If you are solving these coding problems to prepare for software engineer job interviews, you can also take a look at following books. They contain wealth of knowledge and several frequently asked coding problems from Java and C++ interviews :- Programming Interviews Exposed: Secrets to Landing Your Next Job (book)
- Coding Puzzles: Thinking in code By codingtmd (book)
- Cracking the Coding Interview: 150 Programming Questions and Solutions (book)
21 comments :
Olivier Chorier said...First solution : you should start j from i+1 instead of 0.Second/Third solution : Take care about memory consumption (we have two collections in memory)
Unknown said...How do you guarantee that two different values stored in the list do not have the same hash ?
Unknown said...It doesn't matter if two different values have the same hash if you are using a class such as HashSet or HashMap. The value of the hash indicates a set into which the entry will be placed, not the actual location of the entry in the set. At least, that is how I read the JavaDoc for the classes.
javin paul said...@Olivier, valuable suggestions, you must be a good code reviewer :)
javin paul said...@Luca, seems Bradley Ross has already answered your question, thanks Ross. If you are interested upon how those Hash classes work in Java, I suggest you to take a look at my another post How HashSet internally works in Java
sapan said...what is the best way using Java8?
javin paul said...@sapan, you mean best way to learn Java8? or you mean using it on your real project?In case of first try solving programming problems like this using Java 8, for the second use Java 8 with collections and streams.
Arun Gupta said...I am a bit confused here with the first solution.How does it guarantee to return duplicate values without first sorting the array?
Unknown said...Any idea without using extra space ?
javin paul said...Hello @Vivek, the first solution is without additional space but time complexity is O(n^2), I don't think there is a solution with O(n) without any additional space.
Anonymous said...HI, For the first 2 solutions , logic is not may be correct for the below inputs,String[] names = { "Java","Java", "JavaScript", "Python", "C", "Ruby", "Java" };
Anonymous said...public class App { public static void main(String[] args) { String[] names = { "Java", "JavaScript", "Python", "C", "Ruby", "Java" }; List listStr = Arrays.asList(names); Integer dupCount = 0; StringBuilder dupvalues = new StringBuilder(); Map map = new HashMap<>(); for (String value : listStr) { int times = Collections.frequency(listStr, value); if(map.containsKey(value)){ dupCount++; map.put(value, String.valueOf(times)); dupvalues.append(value).append(","); }else{ map.put(value, String.valueOf(times)); } } System.out.println(map); }}
Ram said...How to find duplicate number from 1 to N.ex.input is 1 to 40output-11,22,33
Anonymous said...List iList = new ArrayList<>(); for (String s : sarr){ iList.add(s); } for (String elem : iList){ if (Collections.frequency(iList, elem) > 1) { System.out.println("Got one : " + elem); } }time and space complexity both O(n)
javin paul said...@Anonymous good solution. didn't know about the Collections.frequency() method, is that newly added?
Unknown said...it will give us wrong answer if we have a element more than two times.for example - if java is three times in array then in answer it will print two times
Unknown said...public List getDuplicateArray(String[] in){Map indexMap=null; List indexArray=null; indexMap=new HashMap<>(); for(String n:in) { if(indexMap.containsKey(n)) { indexMap.put(n,2); }else { indexMap.put(n,1); } indexArray=new ArrayList(); for(Integer key:indexMap.keySet()) { if(indexMap.get(key)>1) { indexArray.add(key); }}return indexArray;
Satya said...public List getDuplicateArray(String[] in){Map indexMap=null; List indexArray=null; indexMap=new HashMap<>(); for(String n:in) { if(indexMap.containsKey(n)) { indexMap.put(n,2); }else { indexMap.put(n,1); } indexArray=new ArrayList(); for(Integer key:indexMap.keySet()) { if(indexMap.get(key)>1) { indexArray.add(key); }}return indexArray;
eran_ariel said...thank you :)
Unknown said...public static void main(String[] args) { int arr[]={1,1,1,5,3,3,6,4,3,4,7,3,2,9,3,2,4,1}; for (int i = 0;i < arr.length; i++) { for(int j = i + 1; j < arr.length; j++) { if(arr [i]==arr [j]) System.out.println("duplicate value sorting="+arr[j]); } } } } i can't find how to resolve it & can't seprate the value of duplicate
Anonymous said...Arr[i]
Post a Comment
Newer Post Older Post Home Subscribe to: Post Comments ( Atom )Best System Design and Coding Interview Resources
System Design & Interview Prep
- ByteByteGo Lifetime Plan (50% OFF)
- Codemia Lifetime Plan (60% OFF)
- Exponent Annual Plan (70% OFF)
- Educative Premium Plus (55% OFF)
- DesignGurus All Course Bundle (55% OFF)
- Everything Java Interview Bundle (50% OFF)
- 101 Blockchain (50% OFF)
- Vlad Mihalcea's High Performance Bundle (50% OFF)
- Javarevisited Substack Subscription (50% OFF)
- Head First Software Architecture (Book)
Best Online Learning Resources and Platforms
- Coursera Plus Discount (40% OFF)
- Datacamp Discount (50% OFF)
- AlgoMonster Discount (50% OFF)
- Udemy Discount (80% OFF)
- Baeldung Discount (33% OFF)
- LabEx Discount (50% OFF)
- Codecademy Discount (50% OFF)
- Udacity Discount (50% OFF)
- ZTM Academy Discount (20% OFF)
- Frontend Masters Discount (10% OFF)
- Whizlabs Discount (30% OFF)
Search This Blog
Preparing for Java and Spring Boot Interview?
- Join My Newsletter .. Its FREE
My Books
- Everything Bundle (Java + Spring Boot + SQL Interview) for a discount
- Grokking the Java Interview
- Grokking the Spring Boot Interview
- Spring Framework Practice Questions
- Grokking the SQL Interview
- Grokking the Java Interview Part 2
- Grokking the Java SE 17 Developer Certification (1Z0-829 Exam)
My Courses
- Spring Professional Certification Practice Test
- Java SE 11 Certification Practice Test
- Azure Fundamentals AZ-900 Practice Test
- Java EE Developer Exam Practice Test
- Java Foundations 1Z0-811 Exam Practice Test
- AWS Cloud Practitioner CLF-C02 Exam Practice Test
- Java SE 17 1Z0-829 Exam Practice Test
- Azure AI-900 Exam Practice Test
- 1Z0-829 Java Certification Topic wise Practice Test
My Newsletter articles
- How to grow financially as Software Engineer?
- Difference between Microservices and Monolithic Architecture
- What is Rate Limiter? How does it work
- Difference between @Component vs @Bean annotations in Spring Framework?
- Top 10 Software Design Concepts Every Developer should know
- How does SSO Single Sign On Authentication Works?
Interview Questions
- core java interview question (183)
- interview questions (113)
- data structure and algorithm (99)
- Coding Interview Question (88)
- spring interview questions (52)
- design patterns (48)
- object oriented programming (42)
- SQL Interview Questions (37)
- thread interview questions (30)
- collections interview questions (26)
- database interview questions (16)
- servlet interview questions (15)
- hibernate interview questions (11)
- Programming interview question (6)
Java Tutorials
- FIX protocol tutorial (16)
- JDBC (35)
- Java Certification OCPJP SCJP (33)
- Java JSON tutorial (23)
- Java Programming Tutorials (21)
- Java multithreading Tutorials (64)
- Java xml tutorial (16)
- date and time tutorial (27)
- java IO tutorial (30)
- java collection tutorial (94)
- jsp-servlet (37)
- online resources (227)
Get New Blog Posts on Your Email
Get new posts by email:
SubscribeFollowers
Categories
- courses (348)
- SQL (80)
- linux (54)
- database (53)
- Eclipse (38)
- REST (34)
- Java Certification OCPJP SCJP (33)
- JVM Internals (27)
- JQuery (26)
- Testing (24)
- Maven (16)
- general (16)
Blog Archive
- ► 2026 (77)
- ► February (41)
- ► January (36)
- ► 2024 (185)
- ► December (14)
- ► November (3)
- ► October (35)
- ► September (28)
- ► August (10)
- ► July (20)
- ► June (7)
- ► May (24)
- ► April (30)
- ► March (9)
- ► February (1)
- ► January (4)
- ► 2023 (511)
- ► December (4)
- ► November (4)
- ► October (7)
- ► September (149)
- ► August (20)
- ► July (43)
- ► June (2)
- ► May (151)
- ► April (91)
- ► March (27)
- ► February (8)
- ► January (5)
- ► 2022 (69)
- ► December (1)
- ► September (1)
- ► August (20)
- ► July (20)
- ► June (7)
- ► May (5)
- ► April (4)
- ► March (3)
- ► February (6)
- ► January (2)
- ► 2021 (251)
- ► December (1)
- ► November (5)
- ► October (14)
- ► September (34)
- ► August (96)
- ► July (101)
Contributors
- SimpleJava
- javin paul
Popular Posts
-
I Joined 20+ Generative AI Online Courses — Here Are My Top 6 Recommendations for 2026 Hello guys, If you are following what's going on Tech then you may know that Generative AI has fundamentally changed the whole tech land...
-
How to create HTTP Server in Java - ServerSocket Example Java has very good networking support, allows you to write client-server applications by using TCP Sockets. In this tutorial, we will learn... -
How HashMap works in Java? Hello guys, if you are looking for an answer of popular Java interview question, how HashMap works in Java? or How HashMap works internally ...
-
The 2025 Java Developer RoadMap [UPDATED] Hello guys, I have been sharing a lot of roadmaps to become a Web developer , DevOps engineer , and recently React.js developer . One of th... -
Is ByteByteGo worth it in 2026 for System Design Interview Prep? Hello guys, when it comes to preparing for technical interviews, few challenges intimidate developers as much as system design . While cod...
-
3 ways to solve java.lang.NoClassDefFoundError in Java J2EE I know how frustrating is to see "Exception in thread "main" java.lang.NoClassDefFoundError" while running your Java ...
-
DesignGurus.io or Bugfree.ai? Which is Better Place to Prepare for System Design Interview in 2026? Hello guys, preparing for coding interviews in 2026 is much more competitive than ever. The tech market is flooded with more qualified can...
-
Pluralsight vs CodeCademy Review 2025? Which is better to learn Programming and Development? As a programmer, the most important thing is to keep yourself up-to-date. If you don't, your skills will become obsolete, and you may n... -
Top 133 Java Interview Questions Answers for 2 to 5 Years Experienced Programmers Time is changing and so is Java interviews. Gone are the days, when knowing the difference between String and StringBuffer can help you to g... -
I Tested 30+ Websites to Learn Java: Here Are My Top 6 Recommendations for 2026 Hello guys, I’ve spent the last 48 months systematically testing and evaluating 30+ websites, courses, and platforms for learning Java. Not ...
Subscribe To
Pages
- Privacy Policy
- Terms & Conditions
Tag » How To Find Duplicate Number In Array Java
-
Program To Print The Duplicate Elements Of An Array - Javatpoint
-
Find Duplicates In O(n) Time And O(1) Extra Space | Set 1
-
Java Array, Finding Duplicates - Stack Overflow
-
[Solved] 2 Ways To Find Duplicate Elements In A Given Array In Java
-
Java: Find The Duplicate Values Of An Array Of Integers - W3resource
-
Find The Duplicate Number - LeetCode
-
Find A Duplicate In An Array - Medium
-
How To Detect Duplicate Values In Primitive Java Array? - Tutorialspoint
-
Find Duplicate Elements In An Array Java Program | Tech Tutorials
-
Find Duplicate Elements In An Array In Java - Apps Developer Blog
-
Check For Duplicates In An Array In Java - Techie Delight
-
Find Duplicate Number In An Array Java Code Example - Code Grepper
-
How To Get Duplicate Element From An Array In Java? - YouTube
-
How To Find Duplicate Value In An Array In Java? - W3schools.blog