Botre Posted May 5, 2015 Share Posted May 5, 2015 (edited) mean median mode range package org.bjornkrols.math; import java.util.*; /** * Created by Bjorn on 5/05/2015. */ public final class Statistics { private Statistics() { // This class should never be instantiated. // Do not delete or make accessible. } /** * @return The average value. */ public static double mean(int... n) { double total = 0; for (int i : n) total += i; return total / n.length; } /** * @return Midpoint between the lowest and highest value of the set. */ public static double median(int... n) { Arrays.sort(n); int length = n.length; int center = n[length / 2]; return length % 2 == 0 ? mean(n[(length / 2) - 1], center) : center; } /** * @return The most frequently occurring value(s) of the set. */ public static List<Integer> mode(int... n) { Map<Integer, Integer> occurrence = new HashMap<Integer, Integer>(n.length); int maximum = 0; List<Integer> maximumElements = new ArrayList<Integer>(); for (int i : n) { Integer o = occurrence.get(i); if (o == null) occurrence.put(i, o = 1); else occurrence.put(i, o += 1); if (o > maximum) { maximum = o; maximumElements.clear(); maximumElements.add(i); } else if (o == maximum) maximumElements.add(i); } return maximumElements; } /** * @return The span of values over which the data set occurs. */ public static int range(int... n) { int minimum = Integer.MAX_VALUE; int maximum = Integer.MIN_VALUE; for (int i : n) { if (i < minimum) minimum = i; if (i > maximum) maximum = i; } return maximum - minimum; } } Edited May 6, 2015 by Botre Quote Link to comment Share on other sites More sharing options...
Luza army Posted May 5, 2015 Share Posted May 5, 2015 o my god you so smart Quote Link to comment Share on other sites More sharing options...
lare96 Posted May 5, 2015 Share Posted May 5, 2015 cool beans, thanks for this Quote Link to comment Share on other sites More sharing options...
fixthissite Posted May 6, 2015 Share Posted May 6, 2015 Store occurance.get(i) in a variable to avoid excess accesses to the map. Same with length/2, to lower the overhead from calculationsThe mode method is pretty expensive. Try something a little less intense: int mode(int...numbers) { int highestNumber = 0, mostOccurrences = 0; int[] counter = new int[numbers.length]; for(int i = 0; i < counter.length; i++) { int currentNumber = numbers[i]; int occurrences = counter[currentNumber]++; if(mostOccurrences < occurrences) { mostOccurrences = occurrences; highestNumber = currentNumber; } } return highestNumber; } 2 Quote Link to comment Share on other sites More sharing options...
Botre Posted May 6, 2015 Author Share Posted May 6, 2015 (edited) Store occurance.get(i) in a variable to avoid excess accesses to the map. Same with length/2, to lower the overhead from calculations The mode method is pretty expensive. Try something a little less intense: int mode(int...numbers) { int highestNumber = 0, mostOccurrences = 0; int[] counter = new int[numbers.length]; for(int i = 0; i < counter.length; i++) { int currentNumber = numbers[i]; int occurrences = counter[currentNumber]++; if(mostOccurrences < occurrences) { mostOccurrences = occurrences; highestNumber = currentNumber; } } return highestNumber; } Thanks for the feedback! (updated) Edited May 6, 2015 by Botre Quote Link to comment Share on other sites More sharing options...