猿问

如何仅使用基本循环删除 int 数组的重复项

该代码需要删除数组中的所有重复整数,但要保持简单。如果没有 system.arraycopy 或任何复杂的东西,就不能使用 set 或任何“超出”知识循环的东西。它是一个接收数字数组的构造函数,应该检查数字是否重复并创建一个没有重复的数组


例如:


{1,2,5,5,2,4,5,1} 将是 {1,2,5,4} 所以它应该创建一个数字大小不重复的数组。


试图创建一个循环来计算数字是否重复本身,只有如果不是,它才会添加它,但这不是最好的主意,因为它不会计算至少重复一次的数字


int repeat = 0;enter code here

        int counter = 0;

        if(se.length < 2)

        {

            System.out.println("Array smaller than 2, please change the size!");

            return;

        }

        for(int i = 0; i < se.length; i++)

        {

            for(int j = 0; j < se.length; j++)

                if(se[i] == se[j])

                {

                    repeat++;

                }

            if(repeat == 0)

                counter++;

            repeat = 0;

        }


RISEBY
浏览 98回答 2
2回答

九州编程

您可以尝试以下方法:public static void removedups(int se[]) {&nbsp; &nbsp; int repeat = 0;&nbsp; &nbsp; int counter = 0;&nbsp; &nbsp; if(se.length < 2) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Array smaller than 2, please change the size!");&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; //Find out all the duplicates and replace with 0&nbsp; &nbsp; boolean isZeroPresent = false;&nbsp; &nbsp; int zeroindex = -1;&nbsp; &nbsp; for(int i = 0; i < se.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; for(int j = i + 1; j < se.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(se[i] == se[j]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(se[i] == 0 && !isZeroPresent) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isZeroPresent = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zeroindex = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; se[j] = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; //find the exact count of the array which does not contains duplicates&nbsp; &nbsp; int customIndex = 0;&nbsp; &nbsp; for(int i = 0; i < se.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(se[i]);&nbsp; &nbsp; &nbsp; &nbsp; if(isZeroPresent && zeroindex == i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; customIndex++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(se[i] == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; customIndex++;&nbsp; &nbsp; }&nbsp; &nbsp; //create new array which will hold all the unique values and return&nbsp;&nbsp; &nbsp; int arr[] = new int[customIndex];&nbsp; &nbsp; int j = 0;&nbsp; &nbsp; for(int i = 0; i < customIndex; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if(se[i] == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(zeroindex == i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[j] = se[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; arr[j] = se[i];&nbsp; &nbsp; &nbsp; &nbsp; j++;&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println("-----------------");&nbsp; &nbsp; printArr(arr);}public static void printArr(int arr[]) {&nbsp; &nbsp; for(int i = 0; i < arr.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(arr[i]);&nbsp; &nbsp; }}

BIG阳

诀窍是有两个索引,一个从 ( i) 读取,一个数组部分的结果索引 ( ) 保持唯一元素。writingIint[] removeDuplicates(int[] a) {&nbsp; &nbsp; int writingI = 0;&nbsp; &nbsp; // Invariant condition:&nbsp; &nbsp; //&nbsp; &nbsp; &nbsp;writingI <= i&nbsp; &nbsp; //&nbsp; &nbsp; &nbsp;a[0 <= j < writingI] are all unique&nbsp; &nbsp; for (int i = 0; i < a.length; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; int n = a[i];&nbsp; &nbsp; &nbsp; &nbsp; boolean found = false;&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < writingI; ++j) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (a[j] == n) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; found = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (!found) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a[writingI] = n;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++writingI;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; //return Arrays.copyOf(a, 0, writingI);&nbsp; &nbsp; int[] result = new int[writingI];&nbsp; &nbsp; for (int j = 0; j < writingI; ++j) {&nbsp; &nbsp; &nbsp; &nbsp; result[j] = a[j];&nbsp; &nbsp; }&nbsp; &nbsp; return result;}我向你展示了这个技巧是如何运作的,我的良心不好。它确实有助于自己挖掘问题并找到解决方案。
随时随地看视频慕课网APP

相关分类

Java
我要回答