1

This is a simple Java problem that's been puzzling me really hard.

public static int getMode(int[] a){...} is a method that, when passed a sorted array of ints with one or more elements, returns the element that appears most often.

Example:

int[] a={0, 0, 0, 1, 1, 2, 7, 9, 9, 15, 15, 15, 15, 15, 15};

Should return 15, because it appears more times than any other element. If there's more than one element with the maximum incidence, the method should return the one that appears first in the array.

Example:

int[] b={2, 2, 3, 3, 4, 5, 6};

Should return 2, not 3.

I know the logic of the method in my head, but I can't translate it into code. Can someone help?

Matt
  • 43,482
  • 6
  • 101
  • 102
EddieTheEd
  • 31
  • 1
  • 4

0 Answers0