Question

Write a method maxOccurrences that accepts a list of integers as a parameter and returns the...

Write a method maxOccurrences that accepts a list of integers as a parameter and returns the number of times the most frequently occurring integer (the “mode”) occurs in the list. Solve this problem using a map as auxiliary storage. If the list is empty, return 0.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

METHODS ARE WRITEN IN  TWO DIFFERENT WAYS:

METHOD 1 :

public int maxOccurrences(List<Integer> list)

{

HashMap<Integer , Integer> map = new HashMap<Integer, Integer>();

int modeVal = 0;

for(int n : list)

{

if(map.containsKey(n))

{

map.put(n, map.get(n) + 1);

}

else

{

map.put(n, 1);

}

if(map.get(n) > modVal)

modeVal = map.get(n);

}

return modeVal;

}

METHOD 2:

public static int maxOccurences(List<Integer> list)

{

Map<Integer, Integer> map = new HashMap<Integer, Integer>();

Iterator<Integer> i = list.iterator();

while(i.hasNext())

{

int num = i.next();

if(map.containsKey(num))

{

map.put(num, map.get(num) + 1);

}

else

{

map.put(num,1);

}

}

set<Integer> setKey = map.keySet();

i = setKey.iterator();

int max = 0;

while(i.hasNext())

{

int current = map.get(i.next());

if(current > max)

{

max = current;

}

}

return max;

}

Add a comment
Know the answer?
Add Answer to:
Write a method maxOccurrences that accepts a list of integers as a parameter and returns the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Write a static method named mode that takes an array of integers as a parameter...

    1. Write a static method named mode that takes an array of integers as a parameter and that returns the value that occurs most frequently in the array. Assume that the integers in the array appear in sorted order. For example, if a variable called list stores the following values: ist -3, 1, 4, 4, 4, 6, 7,8, 8, 8, 8, 9, 11, 11, 11, 12, 14, int 141i Then the call of mode (li array, appearing four times. st,...

  • Function name: triangular Parameters: integer Returns: a list Description: Write a function called triangular that accepts...

    Function name: triangular Parameters: integer Returns: a list Description: Write a function called triangular that accepts one integer parameter that returns a list with the same number of terms as the value of the parameter. If the parameter is zero or a negative number, you should return an empty list A triangular number is the sum of a positive integer and all the integers before it. The series is as follows: 1, 3, 6, 10, 15, 21,

  • Write a method named factorial that accepts an integer n as a parameter and returns the...

    Write a method named factorial that accepts an integer n as a parameter and returns the factorial of n, or n!. A factorial of an integer is defined as the product of all integers from 1 through that integer inclusive. For example, the call of factorial(4) should return 1 2 3 4, or 24. The factorial of 0 and 1 are defined to be 1. You may assume that the value passed is non-negative and that its factorial can fit...

  • Write a method that given a non-empty ArrayList of Integers parameterand also an Integer parameter, the...

    Write a method that given a non-empty ArrayList of Integers parameterand also an Integer parameter, the method returns a new ArrayList containing the elements from the original ArrayList that come after the first occurrence of Integer parameter in the original ArrayList. If the original ArrayList does not contain an occurrence of the Integer parameter return an empty ArrayList . The original ArrayList must be unaffected by the method execution. Following are some examples of the invocation of the method using...

  • Submit Chapter7.java with four (4) public static methods as follows: A. Write a method called mostCommon...

    Submit Chapter7.java with four (4) public static methods as follows: A. Write a method called mostCommon that accepts an array of integers as its only parameter, and returns the int that occurs most frequently. Break ties by returning the lower value For example, {1,2,2,3,4,4} would return 2 as the most common int. B. Write mostCommon (same as above) that accepts an array of doubles, and returns the double that occurs most frequently. Consider any double values that are within 0.1%...

  • Python code that: Accepts a list of integers and an integer, and returns the index of...

    Python code that: Accepts a list of integers and an integer, and returns the index of the SECOND  occurrence of the integer in the list. It returns None if the number does not occur two or more times in the list. Example 1: second_index([2,34,3,45,34,45,3,3], 3) returns 6 Example 2: second_index([2,34,3,45,34,45,3,3], 45) returns 5 Example 3: second_index([2,34,3,45,134,45,3,3], 134) returns None Example 4: second_index([2,34,3,45,134,45,3,3], 100) returns None

  • def second_index(a_list, number): Accepts a list of integers and an integer, and returns the index of...

    def second_index(a_list, number): Accepts a list of integers and an integer, and returns the index of the SECOND occurrence of the integer in the list. It returns None if the number does not occur two or more times in the list. Example 1: second_index ([2,34,3,45,34,45,3,3), 3) returns 6 Example 2: second_index( [2,34,3,45,34,45,3,3), 45) returns 5 Example 3: second_index ( [2,34,3,45,134,45,3,3), 134) returns None Example 4: second_index( [2,34,3,45, 134,45,3,3), 100) returns None return None def hasEveryLetter(s): #s is a string Returns...

  • Java Program Write a method called reverseBottomHalf that accepts a Stack of integers as a parameter...

    Java Program Write a method called reverseBottomHalf that accepts a Stack of integers as a parameter and reverses only the values in the bottom half of the Stack. For example, if a Stack containing the values [1, 2, 3, 4, 5] were passed in (with 1 at the bottom and 5 at the top), the Stack would be changed to [2, 1, 3, 4, 5] (with 2 at the bottom and 5 at the top) after this method was called...

  • Sum of Numbers Problem: Write a method that accepts an integer argument and returns the sum...

    Sum of Numbers Problem: Write a method that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the method will return the sum of 1, 2, 3, 4, . . . 50. Use recursion to calculate the sum. Demonstrate the method in a program. The program should be called SumOfNumbers.java.

  • java /* Q2 (10 pts): Write a method called method that accepts an integer parameter *...

    java /* Q2 (10 pts): Write a method called method that accepts an integer parameter * * * * and returns a sum of the first n terms of the sequence. * In other words, the method should generate the following sequence: 1 + 1/2 + 1/3 + 1/4 + ... 1/n * For example, method2(2) will return 1.5 since 1+1/2 = 1.5 * method2 (15) will return 3.3182289932289937 * You may assume that the parameter n is nonnegative. */...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT