Skip to content

Instantly share code, notes, and snippets.

@neanias
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neanias/5763f74d1520050fbd36 to your computer and use it in GitHub Desktop.
Save neanias/5763f74d1520050fbd36 to your computer and use it in GitHub Desktop.
public class Mode {
public static void main(String[] args) {
int[] dataset = new int[args.length];
int[] count = new int[10];
int mode;
for (int i = 0; i < args.length; i++) {
dataset[i] = Integer.parseInt(args[i]);
}
for (int i = 0; i < dataset.length; i++) {
count[dataset[i]]++;
}
for (int i = 0; i < count.length; i++) {
System.out.printf("[%ds: %d] ", i, count[i]);
for (int j = 0; j < count[i]; j++) {
System.out.print(".");
}
System.out.println("");
}
mode = 0;
for (int i = 0; i < count.length; i++) {
if (count[i] > mode) {
mode = i;
}
}
System.out.printf("Mode: %d\n", mode);
}
}
$ javac Mode.java
$ java Mode 1 1 1 7 7 8 3 9 8 8 8
[0s: 0]
[1s: 3] ...
[2s: 0]
[3s: 1] .
[4s: 0]
[5s: 0]
[6s: 0]
[7s: 2] ..
[8s: 4] ....
[9s: 1] .
Mode: 7 # should be 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment