Home > Software design >  Java find missing numbers in array
Java find missing numbers in array

Time:10-29

I am trying to learn Java on my own for only 3 weeks (from YouTube videos and blogs) and this is my first language. I want to write a program to find missing number (s) in an ascending integer array. I found a way, but it only works if the last number in the incrementing array is less than 10. Like 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.

I also found other programs on the internet, but they all have the same problem.

I tried to write on my own with my limited 3 week knowledge and succeeded. But I think I took the long way. The code is almost 27 lines long.

Let me show you the code I have an integer array with 9 elements: [12, 13, 17, 18, 20, 21, 24, 25, 26] and 14, 15, 16, 19, 22, 23 are missing

public class Exercises {

    public static void main(String[] args) {
        int[] arr = {12, 13, 17, 18, 20, 21, 24, 25, 26};
        int len = arr.length;
        System.out.println("\nArray source: \n"   Arrays.toString(arr));
        
        //for avoiding ArrayIndexOutOfBoundsException
        //error I am creating temp. array with 10 elemets
        //and adding main array elements to temp. array
        int[] tempArr = new int[len   1];
        for (int i = 0; i < len; i  ) {
            tempArr[i] = arr[i];
        }
        
        //adding last element to temp. array
        int max = 0;
        for (int i = 0; i < tempArr.length; i  ) {
            if (tempArr[i] > max) {
                max = tempArr[i];
            }
        }
        tempArr[tempArr.length - 1] = max   1;
        


        System.out.println("\nMissing number(S): ");
        for (int i = 0; i < len - 1; i  ) {
            // If it comes to the last loppf main array
            // this will be use temp. arrays' last element to
            // compare main array
            if (i == (len - 1) && (tempArr[i   1] - arr[i]) > 1) {
                System.out.println(tempArr[i]);
            } else if ((arr[i   1] - arr[i]) > 1) {
                for (int a = 1; a <= (arr[i   1] - arr[i]) - 1; a  ) {
                    System.out.println(arr[i]   a);
                }
            }
        }
    }
}
Output:
Array source: 
[12, 13, 17, 18, 20, 21, 24, 25, 26]

Missing number(S): 
14
15
16
19
22
23

I got what I wanted, but is there a more optimal way to do that?

Btw if I want to make code more esthetic it becomes huge :D

public class Exercises {

    public static void main(String[] args) {
        int[] arr = {12, 13, 17, 18, 20, 21, 24, 25, 26};
        int len = arr.length;
        int[] tempArr = new int[len   1];
        int[] correctArr = new int[MathUtils.max(arr) - MathUtils.min(arr)   1];
        int countArr = (MathUtils.max(arr) - (MathUtils.max(arr) - MathUtils.min(arr)) - 1);
        for (int i = 0; i < correctArr.length; i  ) {
            countArr  ;
            correctArr[i] = countArr;
        }

        System.out.println("\nArray source: \n"   Arrays.toString(arr));
        System.out.println("Source should be: \n"   Arrays.toString(correctArr));

        for (int i = 0; i < len; i  ) {
            tempArr[i] = arr[i];
        }
        int max = 0;
        for (int i = 0; i < tempArr.length; i  ) {
            if (tempArr[i] > max) {
                max = tempArr[i];
            }
        }
        tempArr[tempArr.length - 1] = max   1;

        int count = 0;
        for (int i = 0; i < len - 1; i  ) {
            if (i == (len - 1) && (tempArr[i   1] - arr[i]) > 1) {
                count  ;
            } else if ((arr[i   1] - arr[i]) > 1) {
                for (int a = 1; a <= (arr[i   1] - arr[i]) - 1; a  ) {
                    count  ;
                }
            }
        }

        if (count == 1) {
            System.out.println("\nThere is only one missing number:");
        } else if (count > 1) {
            System.out.println("\nThere are "   count   " missing numbers:");
        }

        for (int i = 0; i < len - 1; i  ) {
            if (i == (len - 1) && (tempArr[i   1] - arr[i]) > 1) {
                System.out.println(tempArr[i]);
            } else if ((arr[i   1] - arr[i]) > 1) {
                for (int a = 1; a <= (arr[i   1] - arr[i]) - 1; a  ) {
                    System.out.println(arr[i]   a);
                }
            }
        }
    }
}
Output:
Array source: 
[12, 13, 17, 18, 20, 21, 24, 25, 26]
Source should be: 
[12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]

There are 6 missing numbers:
14
15
16
19
22
23

CodePudding user response:

You look to be over complicating things since a simple nested for loop is probably all that you need. The outer loop loops through the array, up to but not including the last number, and the inner loop loops between one item in the array going to the next item, from arr[i] up to arr[i 1]. Note that the inner loop won't "loop" if the two array items are contiguous, and so no if blocks are needed:

public class MissingNumber {
    public static void main(String[] args) {
        int[] arr = {12, 13, 17, 18, 20, 21, 24, 25, 26};
        
        System.out.println("missing numbers");
        for (int i = 0; i < arr.length - 1; i  ) {
            for (int j = arr[i]   1; j < arr[i   1]; j  ) {
                System.out.println(""   j);
            }
        }
    }
}

Output:

missing numbers
14
15
16
19
22
23

That's it

CodePudding user response:

You can use List<Integer> to avoid repeating the same calculation.

static List<Integer> missingNumbers(int[] a) {
    List<Integer> missingNumbers = new ArrayList<>();
    for (int i = 1, length = a.length; i < length;   i)
        if (a[i - 1]   1 < a[i])
            for (int j = a[i - 1]   1; j < a[i];   j)
                missingNumbers.add(j);
    return missingNumbers;
}

public static void main(String[] args) {
    int[] a = {12, 13, 17, 18, 20, 21, 24, 25, 26};
    List<Integer> missingNumbers = missingNumbers(a);
    if (missingNumbers.size() == 1)
        System.out.println("There is only one missing number:");
    else
        System.out.println("There are "   missingNumbers.size()   " missing numbers:");
    for (int n : missingNumbers)
        System.out.println(n);
}

output:

There are 6 missing numbers:
14
15
16
19
22
23

CodePudding user response:

int[] arr = { 12, 13, 17, 18, 20, 21, 24, 25, 26 };
System.out.println("Array Source:");
System.out.println(Arrays.toString(arr)   "\n");
System.out.println("Missing number(s):");
int nextNumber = arr[0]   1;
for (int i = 1; i < arr.length; i  ) {
    while (arr[i] > nextNumber) {
        System.out.println(nextNumber);
        nextNumber  ;
    }
    nextNumber  ;
}

To fill the array with all numbers from min to max you can write

int[] shouldBe = IntStream.range(min, max   1).toArray();

and in this special case

int[] shouldBe = IntStream.range(arr[0], arr[arr.length - 1]   1).toArray();
  • Related