title: Find two closest numbers to zero in given array in Java Script
body: <p>Is there any better solution than these(Solution 1 and Solution 2 )? Can you please suggest.</p>
  
  <p><strong>Solution 1:</strong> </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var arr = [2, 5.5, -3, 10, -1, 1.5];
  
  
  let min1 = arr[0],
    min2 = arr[1];
  
  for (let i = 2; i &lt; arr.length; i++) {
    if (Math.abs(arr[i]) &lt; Math.abs(min1)) {
      min1 = arr[i];
  
    }
    if (Math.abs(min2) &gt; Math.abs(min1)) {
      let temp = min2;
      min2 = min1;
      min1 = temp;
    }
  }
  
  console.log("Closest numbers to Zero = " + min1 + ", " + min2);</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Solution 2:</strong> </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var arr = [2, 5.5, -3, 10, -1, 1.5];
  arr.sort((a, b) =&gt; Math.abs(a) - Math.abs(b));
  console.log("Closest numbers to Zero = " + arr[0] + ", " + arr[1]);</code></pre>
  </div>
  </div>
  </p>