title: The Most frequent Number in an array
body: <p>I have this Array i wrote a function MostFreq that takes an array of integers and return 2 values : the more frequent number in the array and its frequency check this code i worte what do you think ? is there a better way to do it?</p>
  
  <pre><code>static void Main()
  { 
      int [] M={4,5,6,4,4,3,5,3};
      int x;
      int f=MyMath.MostFreq(M,out x );
      console.WriteLine("the most Frequent Item = {0} with frequency = {1}",x,f);
  }
  </code></pre>
  
  <p>=====</p>
  
  <p>in the class Mymath</p>
  
  <pre><code>public static int MostFreq(int[] _M, out int x)
  {
      //First I need to sort the array in ascending order
      int Max_Freq, No_Freq, i, k;
      Array.Sort(_M);                         
      k = _M[0];
      Max_Freq = 0; i = 0; x = 0;
      while (i &lt; _M.Length)
      {
          //No_Freq= the frequency of the current number
          No_Freq = 0;
          //X here is the number which is appear in the array Frequently 
          while (k == _M[i])
          {
              No_Freq++;
              i++;
              if (i == _M.Length) 
                  break;
          }
          if (No_Freq &gt; Max_Freq)
          {
              //so it will be printed the same
              Max_Freq = No_Freq;
              x = k;
          }
          if (i &lt; _M.Length) k = _M[i];
      }
      return (Max_Freq);
  }
  </code></pre>