Skip to content

Instantly share code, notes, and snippets.

@jonathan-irvin
Created April 16, 2015 19:41
Show Gist options
  • Save jonathan-irvin/ef96c4480ae47c0077fe to your computer and use it in GitHub Desktop.
Save jonathan-irvin/ef96c4480ae47c0077fe to your computer and use it in GitHub Desktop.
integer Occurences
import java.util.Arrays;
import java.util.Scanner;
public class Occurrences {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("How many numbers will you enter? ");
int entries = in.nextInt();
int[] numbers = new int[entries];
System.out.print("Enter data: ");
for(int i=0; i<numbers.length; i++){
numbers[i] = in.nextInt();
}
Arrays.sort(numbers);
for (int i=0; i<numbers.length;i++){
int count = 1;
int number = numbers[i];
while (i < (numbers.length - 1) && numbers[i+1] == number){
count++;
i++;
}
System.out.print("There are "+count);
System.out.println(" "+numbers[i]+"s");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment