-2

So, I need to write a code where I print the most frequent character in an array. If there is, only one then just print that character, if there are 2 then print both and if there are none then print no number is missing. I have got it to find the most single frequent value, but I can't figure out how to make it print if there are more than one frequent characters.

public class ArrayOfNumbers {

    public static void main (String []args) {
        int [] array = {2,3,5,1,2,3};
        int n = 6;
        System.out.println(findMostFrequent);
    }

    public static int findMostFrequent(n, array) {
        int counter = 1;
        int tempCounter;
        int mostFrequent = array{0};
        int temp = 0;
        for (int i = 0; i < array.length - 1; i++) {
            temp = a{1};
            tempCounter = 0;
            for (int j = 1; j < array.length; j++) {
                if (temp == a[j]){
                tempCount++
            }
            if (tempCounter > counter){
                mostFrequent = temp;
                counter = tempCounter;
            }
        }
        return mostFrequent;
    }
  }
}
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
newguy
  • 53
  • 3

1 Answers1

1

Your code has lot's of mistakes!

  • Getting the nth item of array is not with a{1} but with a[0]
  • Parsing this value to int and not to int[] (it's a single value)
  • This calling is wrong System.out.println(findMostFrequent);. There are parameters missing
  • Variable tempCount has not been initialized in the findMostFrequent method.
  • You don't need the lenght variable int n at all. You get the array size by array.length.

  • Bad copying from Java - Find the most popular element in int[] array

Community
  • 1
  • 1
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183