Main PageProblems-Solve a Problem printhange 2 O maxCount Favorite Language/Type
ID: 3904095 • Letter: M
Question
Main PageProblems-Solve a Problem printhange 2 O maxCount Favorite Language/Type: Java ArrayintList implementing Related Links ArraylntList.java Author Stuart Reges (on 2014/02/13) Write a method maxCount that returns the number of occurrences of the most frequently occuring value in a sorted list of integers. Because the list will be sorted, all duplicates will be grouped together, which will make it easier to count duplicates. For example suppose that a variable called list stores the followin g sequence of values [1, 3, 4, 7, 7, 7, 7, 9, 9, 11, 13, 14, 14, 14, 16, 16, 18, 19, 19, 19] This list has values that occur just once (1, 3, 4, 11, 13, 18), values that occur twice (9, 16), values that occur three times (14, 19) and a single value that occurs four times (7). Thefore, the following call list.maxCount() should return 4 to indicate that the most frequently occuring value occurs 4 times. It is possible that there wil be a tie for the most frequently occurring value, but that doesn't affect the outcome because you are just returning the count, not the value. For example, if there are no duplicates in the list, then every value will occur exactly once and the maximum would be 1. If the list is empty, your method should return 0 You are writing a method for the ArraylntList class discussed in lecture (handouts 3, 5 and 6) public class ArrayIntList ( private int[] elementData; // list of integers private int size; // current # of elements in the list You are not to call any other ArraylntList methods to solve this problem. Your solution must run in O(n) time. You may not use any auxiliary data structures to solve this problem, although you can have as many simple variables as you like. Remember that you can assume that the values appear in sorted (nondecreasing) order Type your solution here:Explanation / Answer
MaxCount() method for Sorted list of integer:
This method used in ArrayIntList class for to return the number of most frequently occuring value from list of integer.
First create a method named as maxCount and pass list of integer type array.
Using for loop we check how many times an integer occured in list. Then count that occurance value. Then check with other number of occurrences. Then return the highest number of occurrences of the most frequently occuring value.
Following Method maxCount return the number of occurrences of the most frequently occuring value in a sorted list of integer.
public int maxCount(int[] arr)
{
int counter = 0, count = 0;
for (int i = 0; i < arr.length; i++) {
count=0;
for (int j = i; j < arr.length; j++) {
if (arr[i]==arr[j]) {
count++;
}
if(counter<=count)
{
counter=count;
}
}
}
return counter;
}
Output of this method return value according to your requirement. i.e.
? if there is no duplicates value in your given list of integer type array then it returns 1.
e.g:
int[] arr = {1,2,3,4,5};
System.out.printf(maxCount(arr));
output will be: 1
? if your given list of integer type array is empty then it returns 0.
e.g:
int[] arr = {};
System.out.printf(maxCount(arr));
output will be: 0
? if the duplicate values are present in your list of integer type sorted array then it return maximum number of occurrences of the most frequently occuring value in a sorted list of integer.
e.g:
int[] arr = {1,1,2,2,2,3,3,4,5};
System.out.printf(maxCount(arr));
output will be: 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.