Java代码,用于删除多个重复的数组元素

时间:2017-12-24 05:27:29

标签: java arrays optimization duplicates

我的java代码有错误。并且,有人可以纠正这个或建议一些更好的代码选项。 我正在尝试按照以下步骤执行此操作,它适用于数组索引{1,n},但我必须将其转换为{0 to n-1}中的Java

the sequence removes more than one duplicate element of array using this java code.

Java code:我使用下面的代码尝试了上述序列,如果某处出错,请提供帮助。 Java code正在使用数组索引{o to n-1},上述序列适用于{1 to n}

    int[] arr = {3,6,6,5,4,3,4}                //Array which contains duplicate element  for 4 element arr can be { 1, 2, 2, 0}
    int[] A = { 99, 99, 99, 99, 99, 99,99 };
    int[] result ={ 99, 99, 99, 99,99,99,99 };
    int[] r =  { 99, 99, 99, 99,99,99,99 };
    int counter = 0, count = 0;
    for (int i = 0; i < arr.length; i++) {
        boolean isDistinct = false;
        for (int j = 0; j < i; j++) {      
            if (arr[i] == arr[j]) {
                isDistinct = true;
                break;
            }
        }
        if (!isDistinct) {
            result[counter++] = arr[i];
               r[arr[i]]=arr[i];       //result in [0, 1, 2, 99] it will find which index have duplicate element
        }
    }

for (int i = 0; i < counter; i++) {
    count = 0;
    for (int j = 0; j < arr.length; j++) {     // counting the multiple occurrence of array element
        if (result[i] == arr[j]) {
            count++;
        }
      r[result[i]]=count;  // it will result in 1 = 1, 2 = 2,0 = 1 shows which index have how many multiple occurance 
    }
}                           
      // here r will result in{1, 1, 2, 99} and it is the A array of the above example

       int[]b=new int[7];
       int q=0;
       int B[]={99, 99, 99, 99, 99,99,99 };
       for (int i1 = 0; i1 <7; i1++){     // generating b array according to index 
       b[i1]=i1;
       } 
       HashSet<Integer> setOfSecondArr = new HashSet<Integer>();
       for (int i2 : result) {
           setOfSecondArr.add(i2);
        }
       for (int i3 : b) {                     //B array constructed here as in matlab code
            if (setOfSecondArr.add(i3)) {
                B[i3]=i3;
            }
       }
       // B will result [99, 99, 99, 3] means where no single occurance found B is same as B of the above example

for (int i11 = 0; i11 <r.length; i11++)
        {
             if(r[i11]!=99)
                { 
                 r[i11] = arr[i11];        // generating an array that replace r element by arr       
                }
        }
         // this loop will result in [1, 2, 2, 99]

到目前为止,Java代码工作正常,但在构建C Array时,我发现了问题:如果我对arr = {1, 2, 2, 0}进行了更改,那么上述所有Java代码都将生成{{1这个下一个for循环将生成{1,1,2,99}。但是,结果应为{1,2,2,99},以便找不到重复项。

{0,1,2,99}

1 个答案:

答案 0 :(得分:0)

您可以使用标准类来执行此操作(以第一个数组为例):

    List<Integer> ints = Arrays
            .asList(3, 6, 6, 5, 4, 3, 4) //list from array, firt example
            .stream()
            .distinct() //filter out duplicates
            .sorted() // sort
            .map(val -> val - 1) //Minus one
            .collect(Collectors.toList());
    System.out.println(ints);

以上输出[2, 3, 4, 5]

代码只是使用集合和流API来计算不同的集合,排序元素,然后从每个元素中减去1然后转换回列表。