how do you find the mode of an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • EquinoX
    New Member
    • Feb 2007
    • 3

    how do you find the mode of an array

    Here's my nonworking code:

    Code:
    int count = 0;
    		int prevCount = 0;
    		int totalCount = 0;
    		
    		for (int i = 0; i <array.length; i++){
    			for (int j = i + 1; j < array.length; j++){
    				if (array[i] == array[j]){
    					count++;
    					i++;
    				}
    			}
    			
    			if (count >= prevCount){
    				totalCount++;
    			}
    			
    			prevCount = count;
    			
    			count = 0;
    		}
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    What issue are you having with the code? Does it come out too high? Too low? Errors?

    For an easier way to find the mode, look into a HashMap , since they can't have duplicates.

    Comment

    Working...