title: Python Algorithm To Maximize Number of Equal Elements in List
body: <p>I am trying to make an algorithm in Python that will take a list of random numbers from 0 to a 1,000,000 no more than 100 elements in length and will even this array out as much as possible giving me the maximum number of equal elements. This is what I have so far:</p>
  <pre><code>def answer(x):
      diff = max(x) - min(x)
      while diff &gt; 1:
          x[x.index(max(x))] = x[x.index(max(x))] - (diff / 2)
          x[x.index(min(x))] = x[x.index(min(x))] + (diff / 2)
          diff = max(x) - min(x)
      return count(x)
  
  def count(x):
      from collections import Counter
      c = Counter(x)
      return max(c.values())
  </code></pre>
  <p>This will take an array such as [0,50] and create an array [25,25] and return the integer 2 because there are two equal elements in the array. I know for a fact this algorithm works in most cases however it doesn't in all.
  Can anyone please point out any array of integers this would not yield the correct answer for? Thanks</p>
  <h2>Edit:</h2>
  <p>For those who don't want to read the while loop the code finding the range of the entire list. Splitting the range in half and adding half to the min, and subtracting half from the max. It is trying to equalize the entire list while keeping the same sum</p>
  <p>[1,4,1] = [2,3,1] = [2,2,2] = (number of equal elements) <code>3</code>
  [2,1,4,9] = [2,5,4,5] = [3,4,4,5] = [4,4,4,4] = (number of equal elements) all<code>4</code></p>