Programming Questions

  • Newest
  • Popular Tags
  • Ask Question
  • Mean Mode
    The challenge to find if the mean of an array is equal to the mode seems to be checking for the wrong thing. Isn't the mode the most frequently occurring element? If so, it seems it is checking for the median rather than the mode. Try entering [1,1,2,4] into one of the correct answers. It will return 1 as if the mean and mode are the same. In this case, the mode is 1 and the mean is 2, so it should return 0. I was wondering why so many people had the mode defined as arr[len(arr)/2]
    stevegh posted this question on 8/16/14 | mean, mode, array
  • +
  • 0
  • -
  • Yeah, I agree. Mode is not same as Median. I't really found some nice way of calculating this. Anyway here's my [python] two variations reference. I'm not satisfied by either, even though they seems to do the trick. Short, but somehow ugly: def mode(arr) : m = max([arr.count(a) for a in arr]) return [x for x in arr if arr.count(x) == m][0] if m>1 else -1 Using a dictionary, slightly less ugly: def mode(arr) : f = {} for a in arr : f[a] = f.get(a,0)+1 m = max(f.values()) t = [(x,f[x]) for x in f if f[x]==m] return -1 if m <= 1 else t[0][0]
    Log in to write an answer.