简体   繁体   中英

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();
        while(iter.hasNext()){
            int value = iter.next();
            if("1".equals(value)){
                iter.remove();
            }
            else{
                System.out.println("Missing value:"+value);
            }
        }

Can anyone help me to resolve this?

You are comparing every element with 1

if("1".equals(value))

instead you should keep a counter which will start from your lists first element and then incremented by 1 and perform comparison with this counter.

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.

Try,

List<Integer> integerList = new LinkedList<Integer>();
integerList.add(5);
integerList.add(6);
integerList.add(9);
integerList.add(10);
int first = integerList.get(0);
int last  = integerList.get(integerList.size()-1);
for(int i=first+1; i<last; i++){
    if(!integerList.contains(i))
          System.out.println("Number Not in List : "+i);
}

By getting the first and last element from array you can know the start and end of the array and by iteration over that limit you can get what numbers are missing like below:

    List<Integer> input = new ArrayList<Integer>();
    input.add(5);
    input.add(8);
    int firstElement = input.get(0);
    int lastElement  = input.get(input.size()-1);
    for(int i=firstElement+1, j=2; i<lastElement-1; i++,j++){
        if(!input.contains(i))
              System.out.println("Missing Number : "+i + "(position " + j+")");
    }

As we already know that first element and last element is already present in last, no need to check that so we would be only checking of elements exist between first and last element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM