简体   繁体   English

如何使用Java查找给定序列数组列表中的Missing Elements?

[英]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. 1.我有一个带有起始元素和arraylist限制的Integer数组列表。 Example [5,6,9,10] 范例[5,6,9,10]

2.In which I have to iterate and find the missing element and its position. 2.我必须在其中迭代并找到缺少的元素及其位置。 According to the above example ,my output should be number 7 (position3 ),number 8 (position 4) are missing. 根据以上示例,我的输出应为7号(位置3),缺少8号(位置4)。

3.Now I am getting all the numbers printed instead of getting the missing elements. 3.现在我正在打印所有数字,而不是缺少元素。

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 您正在将每个元素与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. 相反,您应该保留一个计数器,该计数器将从列表的第一个元素开始,然后再递增1,然后与该计数器进行比较。

Suggest you a more efficient way than ArrayList.contains() but more limited: ArrayList.contains()相比,建议您一种更有效的方法,但要限制得多:

    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. 无论如何,这是space for timespace for time ,是否使用取决于您的实际需求。

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. 正如我们已经知道first元素和last元素已经存在于last中一样,不需要检查,因此我们仅检查first和last元素之间是否存在元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找到序列中缺少的元素? - How to find the missing elements in a sequence? Java循环缺少给定数组中的元素 - Java loop missing elements in given array 如何在 Dart 或 Java 中找到给定数组范围内的最小缺失数 - How to find smallest missing number in range with given array in Dart or Java MongoDB:使用Java驱动程序在具有给定属性的数组中查找匹配元素 - Mongodb : find matching elements in an array with given attributes using java driver 如何使用 Java 中的给定字符串值从数组列表中查找最长前缀? - How to find longest prefix from array list using given string value in Java? 如何使用 Java 在给定列表中查找具有重复项的最大数字的索引 - How to find the indexes of the biggest number in a given List with duplicates using Java 如何使用 java 从带有日期列表的数组中检查缺失的日期 - how to check the missing date from array with list of dates using java 如何使用Java查找数组中相似元素的位置 - How to find position of similar elements in array using java 从 java 的数组中找出 n 个缺失元素 - Find out n numbers of missing elements from an array in java 从给定的未排序整数数组中查找最长连续元素序列的长度 - Find the length of the longest consecutive elements sequence from a given unsorted array of integers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM