Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A bunch in an array of numbers means that there are more than one element with t

ID: 3842253 • Letter: A

Question

A bunch in an array of numbers means that there are more than one element with the same value in adjacent indices of the array. Write a Java method that takes an array as parameter, counts the number of bunches in the array. This method will return the total number of bunches in the array. public int countBunches (int [] nums) myArray = [1, 1, 1, 1] countBunches (myArray) rightarrow 1 myArray2 = [5, 6, 7, 8, 7, 1, 10 11, 12] countBunches (myArray2) rightarrow 0 In myArray2, there is no element which repeats in adjacent indices. The element "7" appears two times in the array, but they are not in "neighbor" indices. myArray3 = [5, 6, 7, 7, 8, 1, 10, 11, 12] countBunches (myArray3) rightarrow 1 my Array 3 is very similar to myArray2. However, in myArray3, the element "7" repeats in adjacent indices.

Explanation / Answer

The method is given by:

public int countBunches(int[] nums)
{
int i,j,l,c=0,m;
l=nums.length;
for(i=0;i<l;i++)
{
m=1;//counts the number of occurences of a[i]
for(j=i+1;j<l;j++)
{
if(nums[i]==nums[j])
m++;
else
{
i=j-1;
break;
}
}
if(m>1)
c++;//counts the number of bunches
}
return c;
}