Ad
Code
Diff
  • interface HighLow {
    
        static int[] findLargestAndSmallest(int[] nums) {
            if (isCollectionEmpty(nums)) return null;
    
            return findLargestAndSmallestGeneric(nums);
        }
    
        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;
        }
    }
    • interface HighLow {
    • static int[] findLargestAndSmallest(int[] nums) {
    • if (isCollectionEmpty(nums))
    • return null;
    • else if (hasOneElement(nums))
    • return new int[]{nums[0], nums[0]};
    • else if (hasTwoElements(nums))
    • return sortTwoElements(nums);
    • if (isCollectionEmpty(nums)) return null;
    • return findLargestAndSmallestGeneric(nums);
    • }
    • private static int[] findLargestAndSmallestGeneric(int[] nums) {
    • int max = Integer.MIN_VALUE;
    • int min = Integer.MAX_VALUE;
    • int max = nums[0];
    • int min = nums[0];
    • for (int num : nums) {
    • if (max < num)
    • max = num;
    • if (min > num)
    • min = num;
    • if (max < num) max = num;
    • if (min > num) min = num;
    • }
    • return new int[]{min, max};
    • }
    • static boolean hasOneElement(int[] nums) {
    • return nums.length == 1;
    • }
    • static boolean hasTwoElements(int[] nums) {
    • return nums.length == 2;
    • }
    • private static int[] sortTwoElements(int[] nums) {
    • return (nums[0] <= nums[1])
    • ? new int[]{nums[0], nums[1]}
    • : new int[]{nums[1], nums[0]};
    • }
    • private static boolean isCollectionEmpty(int[] nums) {
    • return nums == null || nums.length == 0;
    • }
    • }
Algorithms
Logic
Search
Strings
Data Types
Code
Diff
  • fun findFirstSubString(string: String, subString: String): Int? =
        if (subString.isEmpty()) null
        else string.indexOf(subString).let { if (it == -1) null else it }
    • fun findFirstSubString(string: String, subString: String): Int? =
    • if (subString.isEmpty()) null
    • else subString.toRegex().find(string)?.range?.first
    • if (subString.isEmpty()) null
    • else string.indexOf(subString).let { if (it == -1) null else it }
Algorithms
Logic
Search
Strings
Data Types
Code
Diff
  • fun findFirstSubString(string: String, subString: String): Int? =
        if (subString.isEmpty()) null 
        else subString.toRegex().find(string)?.range?.first
    • fun findFirstSubString(string: String, subString: String): Int? {
    • val stringArray = string.toList()
    • val substringArray = subString.toList()
    • if (stringArray.size < substringArray.size || stringArray.isEmpty() || substringArray.isEmpty()) {
    • return null
    • }
    • var counter = 0
    • var startIndex = 0
    • var i = 0
    • while (i < stringArray.size) {
    • if (stringArray[i] == substringArray[counter]) {
    • if(counter == 0) {
    • startIndex = i
    • }
    • if ((counter + 1) == substringArray.size) {
    • return startIndex
    • }
    • counter++
    • i++
    • }
    • else {
    • if (counter > 0) {
    • i = startIndex + 1
    • }
    • else {
    • i++
    • }
    • counter = 0
    • startIndex = 0
    • }
    • println(i)
    • }
    • return null
    • }
    • fun findFirstSubString(string: String, subString: String): Int? =
    • if (subString.isEmpty()) null
    • else subString.toRegex().find(string)?.range?.first