How To Find Duplicate Elements In An Array In Java?

How To Find Duplicate Elements In An Array In Java?

There are many methods through which you can find the duplicate elements in the given array. In this post, I have discussed two of them. One is using Brute Force Method and another one is using HashSet.
1) Using Brute Force Method :
In this method, we compare each element of an array with other elements. If any two elements are found equal, we declare them as duplicates. The performance of this method is very low if an array contains lots of elements. Therefore, this method is not recommended in real time. It gives time complexity of O(n^2).
2) Using HashSet :
This method is better than the Brute Force method. It gives O(n) performance. You know that HashSet holds only unique elements. It never allows duplicate elements. We use this property of HashSet to find duplicates in an array. What we do is we try to add each element of an array into HashSet using add() method. This method adds only unique elements into HashSet. If you try to add duplicate element, it will return false.

Java Program To Find Duplicate Elements In An Array Using Brute Force Method :

public class DuplicatesInArray {
 public static void main(String[] args) {
  String[] strArray = { "abc", "def", "mno", "xyz", "pqr", "xyz", "def" };

  for (int i = 0; i < strArray.length - 1; i++) {
   for (int j = i + 1; j < strArray.length; j++) {
    if ((strArray[i].equals(strArray[j])) && (i != j)) {
     System.out.println("Duplicate Element is : " + strArray[j]);
    }
   }
  }
 }
}
Output :
Duplicate Element is : def
Duplicate Element is : xyz

Java Program To Find Duplicate Elements In An Array Using HashSet :

import java.util.HashSet;

public class DuplicatesInArray {
 public static void main(String[] args) {
  String[] strArray = { "abc", "def", "mno", "xyz", "pqr", "xyz", "def" };

  HashSet<String> set = new HashSet<String>();

  for (String arrayElement : strArray) {
   if (!set.add(arrayElement)) {
    System.out.println("Duplicate Element is : " + arrayElement);
   }
  }
 }
}
Output :
Duplicate Element is : xyz
Duplicate Element is : def




EmoticonEmoticon