Find Duplicate Elements In An Array using Java

Find Duplicate Elements In An Array using Java

Problem Statement:

Write a Java program to find duplicate elements in an array. For example, duplicate element in {"ONE", "TWO", "THREE", "TWO"} is "TWO".


public class FindDuplicatesInArray { public static void main(String[] args) { String[] Array = {"abc", "def", "mno", "xyz", "pqr", "xyz", "def"}; for (int i = 0; i < Array.length-1; i++) { for (int j = i+1; j < Array.length; j++) { if( (Array[i].equals(Array[j])) && (i != j) ) { System.out.println("Duplicate Element : "+Array[j]); } } } } }


Duplicate Element : def Duplicate Element : xyz


In this program, we compare each and every element of an array with other elements. If the program founds any two elements are equal, the program declare them as duplicates. Time Complexity of this program is O(n^2).


Need Programming or any Technical Related Books, Courses or Help Chat with us


Deals of the Day
Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online

Search Tags

    Java code to find duplicate words in Array