import java.util.*;
import java.lang.*;
import java.io.*;


class Main {
    public static int closestToZero(int[] ints){
        if(ints == null || ints.length==0)
    		return 0;
    
    	int closest = ints[0];
            for(int i = 1 ; i<ints.length; i++){
                 if(Math.abs(ints[i])<=Math.abs(closest))
                        closest = ints[i];
            }
    	return closest;
    }
    
    public static void main(String[] args) {
        System.out.println(closestToZero(new int []{23,6,45,12,21,-1,2}));
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: