Ad
Code
Diff
  • interface HighLow { 
        
        // IMHO with a function this simple and small,
        // seperating it to even more tiny functions is just overkill and makes the code less readable,
        // with such small functions you don't seperate for readability, but reusability. :)
        static int[] findLargestAndSmallest(int[] nums) {
            if (nums == null || nums.length == 0) return null;
    
            int max = nums[0];
            int min = nums[0];
    
            for (int num : nums) {
                if (max < num) max = num;
                if (min > num) min = num;
            }
    
            return new int[]{min, max};
        }
    }
    • interface HighLow {
    • interface HighLow {
    • // IMHO with a function this simple and small,
    • // seperating it to even more tiny functions is just overkill and makes the code less readable,
    • // with such small functions you don't seperate for readability, but reusability. :)
    • static int[] findLargestAndSmallest(int[] nums) {
    • if (isCollectionEmpty(nums)) return null;
    • return findLargestAndSmallestGeneric(nums);
    • }
    • if (nums == null || nums.length == 0) return null;
    • private static int[] findLargestAndSmallestGeneric(int[] nums) {
    • int max = nums[0];
    • int min = nums[0];
    • for (int num : nums) {
    • if (max < num) max = num;
    • if (min > num) min = num;
    • }
    • return new int[]{min, max};
    • }
    • private static boolean isCollectionEmpty(int[] nums) {
    • return nums == null || nums.length == 0;
    • }
    • }