java program to count total duplicate number in given array?

Here we count total number of duplicate number in given array.in duplicateTest method we create two for loop.first for loop value compare two second for loop.if compare value is true then its increase number of count.

				
					public class CountTotalDuplicate {
	public static int duplicateTest(int a[]) {
		int count = 0;
		boolean status = false;
		for (int i = 0; i < a.length; i++) {
			for (int j = i + 1; j < a.length; j++) {
				if (a[i] == a[j]) {
					status = true;
					count++;
				}
			}
		}
		return count;
	}

	public static void main(String[] args) {
		int b[] = { 1, 2, 3, 1, 2, 4, 5, 6 };
		int countDuplicate = duplicateTest(b);
		System.out.println("Total duplicate number:- " + countDuplicate);
	}
}
				
			

Output :-
Total duplicate number:- 2

Leave a Comment

Your email address will not be published. Required fields are marked *