title: Problem Statement - Finding the efficient algorithm and the code which gives less runtime
body: <p>I have this problem statement:</p>
  <p>Two non-negative integers are called siblings if they can be obtained from each other by rearranging the digits of their decimal representations. For example, 123 and 213 are siblings. 535 and 355 are also siblings.</p>
  <p>A set consisting of a non-negative integer N and all of its siblings is called the family of N. For example, the family of <code>553</code> comprises three numbers: <code>355, 535 and 553</code>.</p>
  <p>Write a function:</p>
  <pre><code>class Solution { public int solution(int N); }
  </code></pre>
  <p>that, given a non-negative integer N, returns the largest number in the family of N. The function should return <code>−1</code> if the result exceeds <code>100,000,000</code>.</p>
  <p>For example, given <code>N = 213</code> the function should return <code>321</code>. Given <code>N = 553</code> the function should return <code>553</code>.</p>
  <p>Write an <code>efficient algorithm</code> for the following assumptions:</p>
  <p>N is an integer within the range <code>[0..2,147,483,647]</code>.</p>
  <p>Code snippet #1:</p>
  <pre><code>public static int solution(int N) 
  {
      if (N &lt; 0) 
          throw new ArgumentException(&quot;N should be non-negative number&quot;);
  
      if (N == 0) 
          return 0;
      
      List&lt;int&gt; numbers = new List&lt;int&gt;();
  
      do {
          numbers.Add(N % 10);
          N /= 10;
      } while(N &gt; 0);
      
      numbers.Sort();
      numbers.Reverse();
      
      int result = numbers[0];
  
      for (int i = 1; i &lt; numbers.Count; i++) 
      {
          result = result * 10 + numbers[i];
  
          if (result &gt; 100000000)
              return -1;
      }       
  
      return result;
  }
  </code></pre>
  <p>Code snippet #2:</p>
  <pre><code>public static int solution(int N)
  {
      if (N &lt; 0)
          throw new ArgumentException(&quot;N should be non-negative number&quot;);
  
      if (N &lt; 10)
          return N;
  
      // count the frequency of each digit
      int[] digitFreqs = new int[10];
      do
      {
          digitFreqs[N % 10]++;
          N /= 10;
      } while (N &gt; 0);
  
      // loop through the digit frequencies backwards to build the final answer
      int sol = 0;
      for (int i = digitFreqs.Length; i-- &gt; 0;)
      {
          int digit = i;
          int frequency = digitFreqs[i];
          for (int j = 0; j &lt; frequency; j++)
          {
              sol = sol * 10 + digit;
  
              if (sol &gt; 100000000)
                  return -1;
          }
      }
      return sol;
  }
  </code></pre>
  <p>These code snippets run as expected with the correct output. Among both of these want to know which one is best in terms of efficiency &amp; runtime. Or else any suggestions to improve more using any other algorithm?</p>
  <p>Please help! TIA</p>