Find Duplicates Java Array
There are many algorithm you could to check if existing java array contains duplicate value or not. Below is sample java program which checks array if contains duplicate value or not.
- CheckDuplicateValue.java:
package com.javahonk; public class CheckDuplicateValue { public static void main(String[] args) { String []array = {"Java","Honk","Java","Honk","Java","Honk"}; Boolean duplicate = checkDuplicateArray(array); for (int i = 0; i < array.length; i++) { String string = array[i]; System.out.println("Array value:"+string); } System.out.println("\nIs array contain duplicates:"+duplicate); } private static Boolean checkDuplicateArray(String[] array) { for (int j=0;j<array.length;j++) { for (int k=j+1;k<array.length;k++) { if (array[k].equalsIgnoreCase(array[j])){ return true; } } } return false; } }
- Output:
- For more information on java array please refer oracle documentation here