Java program to find duplicate elements in an array :
In this tutorial, we will learn how to find duplicate elements in an array of strings. We will learn this using two different approaches :
-
Using Brute force method.
-
Using HashSet
In Brute force method, the program will scan each elements one by one. For each element, we will compare it with all other elements of the array. The complexity of this method is O(n^2).
Using one HashSet, we can reduce the complexity to O(n) . Since HashSet holds no duplicate elements, we will try to add all elements of the array to a HashSet. If any addition failed, means that element is already added, we will print that element as duplicate.
Let’s take a look into the program first :
Java program to find duplicate elements in an array :
import java.util.HashSet;
import java.util.Scanner;
public class Main {
//8
private static void findDuplicateUsingBruteForce(String[] array) {
//9
System.out.println("\nDuplicate elements using Brute Force method : \n");
//10
boolean duplicateElementsFound = false;
//11
for (int i = 0; i < array.length - 1; i++) {
for (int j = i + 1; j < array.length; j++) {
//12
if (array[i].equals(array[j])) {
duplicateElementsFound = true;
System.out.print(array[i] + " ");
}
}
}
//13
if (!duplicateElementsFound) {
System.out.println("No duplicate elements found.");
}
}
//15
private static void findDuplicateUsingHashSet(String[] array) {
//16
System.out.println("\nDuplicate elements using HashSet : \n");
//17
HashSet stringSet = new HashSet<>();
//18
boolean duplicateElementsFound = false;
//19
for (String element : array) {
//20
if (!stringSet.add(element)) {
duplicateElementsFound = true;
System.out.println(element + " ");
}
}
//21
if (!duplicateElementsFound) {
System.out.println("No duplicate elements found.");
}
}
public static void main(String[] args) {
//1
String[] wordsArray;
Scanner scanner = new Scanner(System.in);
//2
int count;
//3
System.out.println("Enter number of elements : ");
count = scanner.nextInt();
//4
wordsArray = new String[count];
//5
for (int i = 0; i < count; i++) {
System.out.println("Enter string " + (i + 1) + " : ");
wordsArray[i] = scanner.next();
}
//6
System.out.println("You have entered : ");
for (int i = 0; i < wordsArray.length; i++) {
System.out.print(wordsArray[i] + " ");
}
//7
findDuplicateUsingBruteForce(wordsArray);
//14
findDuplicateUsingHashSet(wordsArray);
}
}
Explanation :
The commented numbers in the above program denotes the step number below :
-
Declare one String array variable to store all user input strings. Create one Scanner object to read user input.
-
Create one int variable to get the total count of the array.
-
Ask the user to enter the total number of elements . Store it in count variable.
-
Create the previously declared array variable. This array is equal to count.
-
Using a for loop, read all elements user enter and store it in the String array.
-
Print all user entered values.
-
Call method findDuplicateUsingBruteForce() to find all duplicate elements in the array using Brute force.
-
This method takes one String array as input . We are passing the String array wordsArray to this method.
-
Find all duplicate elements.
-
Create one boolean duplicateElementsFound and assign it false. This flag will be true if any duplicate element is found.
-
Run two for loops. The outer loop will scan all elements one by one and the inner loop will compare each element with all other elements.
-
If any duplicate element found , assign flag duplicateElementsFound true and print out the element.
-
If duplicateElementsFound is false, means no duplicate element found. Print the message.
-
Call the method findDuplicateUsingHashSet to find duplicate elements using a Hash Set.
-
findDuplicateUsingHashSet takes one array of String elements.
-
Print out all duplicate elements using Hash Set.
-
Create one HashSet of String stringSet.
-
Create boolean flag duplicateElementsFound and assign it value false. true means duplicate elements found and false means no duplicate elements found.
-
Using one for loop , scan all elements one by one.
-
Check if an element can be add to the HashSet or not. It no, means the element is already added and this is a duplicate. Print out the element . Also, assign duplicateElementsFound value true.
-
If no duplicate element is found, print the message.
Sample Output :
Enter number of elements :
5
Enter string 1 :
11
Enter string 2 :
22
Enter string 3 :
33
Enter string 4 :
44
Enter string 5 :
11
You have entered :
11 22 33 44 11
Duplicate elements using Brute Force method :
11
Duplicate elements using HashSet :
11
Enter number of elements :
4
Enter string 1 :
aa
Enter string 2 :
bb
Enter string 3 :
cc
Enter string 4 :
dd
You have entered :
aa bb cc dd
Duplicate elements using Brute Force method :
No duplicate elements found.
Duplicate elements using HashSet :
No duplicate elements found.