Question


1. Write a static method named mode that takes an array of integers as a parameter and that returns the value that occurs mos
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* IDEA: ASSIGN MOST FREQUENT TO BE FIRST ELEMENT THEN LOOP FROM INDEX 1 TO N AND CHECK IF PREVIOUS ELEMENT IS EQUAL TO CURRENT ELEMENT INCREASE COUNT IF SINGLE ELEMENT THE RETURN FIRST OTHERWISE CHECK MAXCOUNT VALUE AND RETURN IT */

static int mostFrequent(int list[], int n)
{
   // find the max frequency using linear
   int maxcount = 1, res = list[0];
   int currcount = 1;
  
   for (int i = 1; i < n; i++)
   {
       if (list[i] == list[i - 1])
           currcount++;
       else
       {
           if (currcount > maxcount)
           {
               maxcount = currcount;
               res = list[i - 1];
           }
           currcount = 1;
       }
   }
  
   // If last element is most frequent
   if (currcount > maxcount)
   {
       maxcount = currcount;
       res = list[n - 1];
   }
  
   return res;
}

/* PLEASE UPVOTE(THANK YOU IN ADVANCE) IF YOU SATISFY WITH THE ANSWER IF ANY QUERY ASK ME IN COMMENT SECTION . I WILL RE-SOLVE THE QUESTION FOR YOU */

Add a comment
Know the answer?
Add Answer to:
1. Write a static method named mode that takes an array of integers as a parameter...
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
  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • 1. Write a complete program based on public static int lastIndexOf (int[] array, int value) {...

    1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...

  • Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...

    Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. To test your function, write a main that prompts a user for a list of 15 integers and outputs the index and value of the first occurrence of the smallest value. The program should print out Enter 15 integers: The position of the first occurrence of the smallest element in...

  • 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.

  • Write a static method named sumOf that takes an array of int values as an argument...

    Write a static method named sumOf that takes an array of int values as an argument (you may safely assume that the array contains at least 1 element). This method should return the sum of all values in the array. Examples: Given the following array declaration and definition. int[] theArray = {0, 0, 0, 0, 0, 0}; sumOf(theArray) will return 0 Given the following array declaration and definition. int[] theArray = {1, 1, 1, 1, 1, 1}; sumOf(theArray) will return...

  • Python: Write a function named "sort_by_average_rating" that takes a list/array of key-value stores as a parameter...

    Python: Write a function named "sort_by_average_rating" that takes a list/array of key-value stores as a parameter where each key-value store has keys "ratings", "budget", and "box_office" where budget and box_office are integers and ratings is a list of integers. Sort the input based on the average of the values in "ratings"

  • Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a...

    Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a method named FillArray to interactively fill the array with any number of values up to 10 or until a sentinel value (999) is entered. If an entry is not an integer, reprompt the user. Call a second method named Statistics that accepts out parameters for the highest value in the array, lowest value in the array, sum of the values in the array, and...

  • daily21. Computing 1. C programming Please provide comments and write pre-condition and post-condition for the function....

    daily21. Computing 1. C programming Please provide comments and write pre-condition and post-condition for the function. Description Create a project called Daily21. Add a source file called daily21.c into the project. In this exercise, you will practice working with an array of integers. In the main function, you first need to create an integer array with 10 elements in t. Use a loop to read 10 integers from the user and store the values to the array (in the order...

  • Write in C. Write a function that takes an array of integers and its size as...

    Write in C. Write a function that takes an array of integers and its size as parameters and prints the two largest values in the array. In this question, the largest value and the second largest value cannot be the same, even if the largest value occurs multiple times. In the first sample, the two largest values are 9 and 8 (even though the value 9 appears twice). In the second sample, all the values are equal and the program...

  • Please write in C# Write a method in C# int[]GetDuplicatesIndices(int[]array), which takes an array of integers...

    Please write in C# Write a method in C# int[]GetDuplicatesIndices(int[]array), which takes an array of integers and returns an array that holds the indices of all the duplicates. Example if the array has the following value 10, 5, 3, 7, 5, 11, 15, 13, 11, 6, 3, 19 then note that the value 5 has duplicate at index 4, value 3 has a duplicate at index 11, and value 7 has a duplicate at index 6. So the method should...

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