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).

Solution Worked 6 UpvotesUpvote


Solution Didn't Worked 0 DownvotesDownvote


Get Programming Help

Play 2048 Game Online

Search Tags

    Java code to find duplicate words in Array