Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jah.tests;
- public class TestMe {
- public static final int MAX_VAL=20;
- public static void countOccurrencesId(int[] arr) {
- int[] counts = new int[MAX_VAL+1];
- //first we work out the count for each one
- for (int i: arr)
- counts[i]++;
- //now we print the results
- for (int i: arr)
- if (counts[i]>0) {
- System.out.println(i+" occurs "+counts[i]+" times");
- //now set this count to zero so we won't get duplicates
- counts[i]=0;
- }
- }
- public static void countOccurrences(int[] arr) {
- int[] counts = new int[MAX_VAL+1];
- //first we work out the count for each one
- for (int i=0; i<arr.length; i++)
- counts[arr[i]]++;
- //now we print the results
- for (int i=0; i<arr.length; i++)
- if (counts[arr[i]]>0) {
- System.out.println(arr[i]+" occurs "+counts[arr[i]]+" times");
- //now set this count to zero so we won't get duplicates
- counts[arr[i]]=0;
- }
- }
- public static void main(String[] args) {
- int[] countMe = { 10, 10, 20, 10, 20, 15 };
- System.out.println("First version:");
- countOccurrencesId(countMe);
- System.out.println();
- System.out.println("Second version:");
- countOccurrences(countMe);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement