Answer:
The method written in java is as follows:
public static int countElements(int[] nums, int target){
int count=0;
for(int i =0;i<nums.length;i++){
if(nums[i] == target){
count++;
}
}
return count;
}
Explanation:
The method definition is not clear in the question you posted; so, I'll answer the question from scratch
This defines the method
public static int countElements(int[] nums, int target){
This initializes count to 0
int count=0;
This iterates through the array
for(int i =0;i<nums.length;i++){
This checks if the current array element is same as the target
if(nums[i] == target){
If yes, the count variable is incremented by 1
count++;
}
}
This returns the count
return count;
}
See attachment for complete program which includes the main