public class findDuplicate { public static void main(String args[]) { String[] myStringArray = { "Alice", "Bob", "Tim", "John", "Tim", "Denice" }; for (int i = 0; i < myStringArray.length; i++) { String toCompare = myStringArray[i]; for (int j = i + 1; j < myStringArray.length; j++) { if (toCompare.equals(myStringArray[j])) { System.out.println("Duplicate Name " + toCompare); } } } } }
Java program to find duplicate elements in an String array.
We have created a sample String array with few String values to check for Duplicate. To Check the Duplicate we need to compare the current String value with rest of the values. The below code approaches this by looping over the array. In the second loop we compare the values with remaining next elements.