0

This is for JAVA programming, by the way.

So, I am trying to code a method where it will return the most frequent int or ints in an array. I already have a whole class set up to find average, sum, and etc., but this specific method is a bit tricky. My code so far is this:

public int[] modeOfData()
{
    List<Integer> modes = new ArrayList<Integer>(  );
    int maxCount=0;
    for (int i = 0; i < data.length; ++i){
        int count = 0;
        for (int j = 0; j < data.length; ++j){
            if (data[j] == data[i]) ++count;
        }
        if (count > maxCount){
            maxCount = count;
            modes.clear();
            modes.add( data[i] );
        } else if ( count == maxCount ){
            modes.add( data[i] );
        }
    }
    return modes.toArray( new Integer[modes.size()] );
}

The return part of this code is the only part that has a syntax error. The syntax error reads: incompatible types. It highlights the part in parentheses. What am I doing wrong, and what must I edit? Thanks! Is there an easier way to code this with just if-elses and loops? Or a way without lists? Thanks again.

3
  • You are returning an List(collection) rather than int.... Commented Nov 15, 2013 at 2:43
  • 1
    i guess public Integer[] modeOfData() would be much appropiate Commented Nov 15, 2013 at 2:45
  • I've updated my answer below to address the second part of your question, which I had overlooked. Commented Nov 15, 2013 at 5:57

2 Answers 2

1

Change return type from int[] to Integer[] in method.

 public Integer[] modeOfData()

Method toArray will return a generic type data. Type int[] is not generic, that's why "The syntax error reads: incompatible types" appears.

You should use Integer[] instead to make the syntax error not displaying.

Below is the source code of toArray method from List.

/**
 * Returns an array containing all of the elements in this list in proper
 * sequence; the runtime type of the returned array is that of the
 * specified array.  Obeys the general contract of the
 * <tt>Collection.toArray(Object[])</tt> method.
 *
 * @param a the array into which the elements of this list are to
 *      be stored, if it is big enough; otherwise, a new array of the
 *      same runtime type is allocated for this purpose.
 * @return  an array containing the elements of this list.
 * 
 * @throws ArrayStoreException if the runtime type of the specified array
 *        is not a supertype of the runtime type of every element in
 *        this list.
 * @throws NullPointerException if the specified array is <tt>null</tt>.
 */
<T> T[] toArray(T[] a);
Sign up to request clarification or add additional context in comments.

Comments

1

OP wrote:
The syntax error reads: incompatible types. It highlights the part in parentheses. What am I doing wrong, and what must I edit?

Ignoring other issues and possible improvements to your actual algorithm, your function is declared as returning an int[], but List<Integer>.toArray(Integer[]) returns an Integer[], which is not an int[].

You will need to either declare your function as returning an Integer[] (probably the simplest for your case), or manually create an int[] and copy the Integer items into it, unboxing one at a time (or you could use Ints.toArray() from Guava I suppose), e.g.

/**
 * Convert collection of Integers to an int array.
 * @param integers Collection of Integers.
 * @return Array of ints, in same order as collection.
 * @throws NullPointerException if integers is null.
 */
static final int[] toIntArray (Collection<Integer> integers) {
    int index = 0, array[] = new int[integers.size()];
    for (Integer integer:integers)
        array[index ++] = integer;
    return array;
}

A bonus side-effect of the former option (return Integer[] instead) is that if you wanted to, you could use a generic type instead of Integer and do comparisons with .equals() and support finding the modes of any collection of objects with properly implemented .equals().

OP wrote:
Is there an easier way to code this with just if-elses and loops? Or a way without lists?

One option is to use a Map<Integer,Integer> that maps element values to their counts. After you go through your input list and tabulate counts, you'll have a map containing every element and how many times it occurred. You have a few options (e.g. track max as you go, or find it afterwards, or sort the list of the map entries by their value) to pull out the maximum counts, which I'll leave as an exercise for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.