Question d’entretien chez Mahathi Infotech

Write a Java program to count how many times a specific target number appears in an array. For example, given the array: [1, 2, 5, 5, 5, 8, 9] and the target number T = 5, the output should be: The number 5 occurs 3 times in the array.

Réponse à la question d'entretien

Utilisateur anonyme

15 avr. 2025

public class CountTargetInArray { public static void main(String[] args) { int[] arr = {1, 2, 5, 5, 5, 8, 9}; int target = 5; int count = 0; for (int num : arr) { if (num == target) { count++; } } System.out.println("The number " + target + " occurs " + count + " times in the array."); } }