# Detect the average interval between time points

I want to detect the average interval between time points. Here is what I have so far. But it doesn’t work well in some cases. Seems there are unwanted 0s(in ‘diffs’ array), which spoil the result.

``public class Testing {        private static int RANDOM_POINT_COUNT = 5          static private long getAverageInterval(long[] times, boolean useSampling) {              long[] diffs;              if (useSampling && times.length > 2 * RANDOM_POINT_COUNT) {                              diffs = calculateAverageRandomly(times, RANDOM_POINT_COUNT);                     } else {                 diffs = calculateAverage(times);             }              long[] sortedDiffs = Arrays.copyOf(diffs, diffs.length);                     Arrays.sort(sortedDiffs);             final long interval = sortedDiffs[sortedDiffs.length / 2];                   return interval;         }          static private long[] calculateAverage(long[] times) {              long[] diffs = new long[times.length - 1];              for (int i = times.length - 2; i >= 0; --i) {                 diffs[i] = Math.abs(times[i] - times[i + 1]);             }              return diffs;         }          static private long[] calculateAverageRandomly(long[] times, int count) {              assert times.length > 1;              if (times.length == 2) {                 return new long[] {times[1] - times[0]};             }              long[] diffs = new long[2*count];             int partCount = (int) Math.round(times.length*1.0/count);             Random rn = new Random();             int currentIndex = 0;              for(int i = times.length - partCount-1; i >= 0 && currentIndex < diffs.length; i-= partCount) {                  int index =  rn.nextInt(partCount) +i;                  if(index == 0) {                     index = 1;                 }                  diffs[currentIndex] = times[index+1] - times[index];                 ++currentIndex;                 diffs[currentIndex] = times[index] - times[index - 1];                 ++currentIndex;             }             return diffs;         }  } ``