import java.util.*; public class SortedFreqs { public int countOccurs(String[] list, String s) { int count = 0; for(int k=0; k < list.length; k++){ if (list[k].equals(s)){ count++; } } return count; } public int[] freqs(String[] names) { Map map = new TreeMap(); for(int k=0; k < names.length; k++){ Integer i = (Integer) map.get(names[k]); if (i == null){ map.put(names[k],new Integer(1)); } else { map.put(names[k],new Integer(i.intValue()+1)); } } int[] result = new int[map.size()]; Iterator it = map.keySet().iterator(); int index = 0; while (it.hasNext()){ Integer i = (Integer) map.get(it.next()); result[index] = i.intValue(); index++; } return result; } public int[] freqs3(String[] names) { Set set = new TreeSet(Arrays.asList(names)); int[] result = new int[set.size()]; int index = 0; Iterator it = set.iterator(); while (it.hasNext()){ String s = (String) it.next(); result[index] = countOccurs(names,s); index++; } return result; } public int[] freqs2(String[] names) { if (names.length == 0){ return new int[0]; } Arrays.sort(names); int count = 1; for(int k=1; k < names.length; k++){ if (! names[k].equals(names[k-1])){ count++; } } int[] result = new int[count]; int runCount = 1; int index = 0; for(int k=1; k < names.length; k++){ if (names[k].equals(names[k-1])){ runCount++; } else { result[index] = runCount; index++; runCount = 1; } } result[index] = runCount; return result; } }