Ilavarasan Jayaraman Ilavarasan Jayaraman - 5 months ago 63x
Java Question

How to find the Missing Elements in a given sequence array list using java?`

1.I have an Integer array list with a starting element and arraylist limit.
Example [5,6,9,10]

2.In which I have to iterate and find the missing element and its position.
According to the above example ,my output should be number 7 (position3 ),number 8 (position 4) are missing.

3.Now I am getting all the numbers printed instead of getting the missing elements.

Below is the code :

public static List<Integer> issue_ret=new ArrayList<>();
Iterator<Integer> iter = issue_ret.iterator();
int value =;
System.out.println("Missing value:"+value);

Can anyone help me to resolve this?


Suggest you a more efficient way than ArrayList.contains() but more limited:

    ArrayList<Integer> list = new ArrayList<>(Arrays.asList(new Integer[]{5, 6, 9, 10}));

    int head = list.get(0);
    int tail = list.get(list.size() - 1);

    int length = tail - head + 1;
    int[] array = new int[length];

    for (int i : list) {
        array[i - head] = 1;

    for (int i = 0; i < array.length; i++) {
        if (array[i] == 0) {
            System.out.println(String.format("Missing %d, position %d", i + head, i + 1));

The limit is: The top Integer number should not be too large. Anyway, it is a space for time way, whether to use depends on your actual needs.