Monday, January 13, 2014

Find member of array closest to 0

I saw this on a test site

my unorthodox answer:


using System;

class Solution
{
    public static double ClosestToZero(double[] ts)
    {
       double? blah = null ;
       for(int eliot=0;eliot<ts.Length;eliot++ )
       {
        blah = blah== null || System.Math.Abs(ts[eliot]) < blah?  System.Math.Abs(ts[eliot]):blah   ;
       
       }
       return (double)blah;
     }
}

No comments:

Post a Comment