Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取int数组中具有重复项的元素_Java_Arrays - Fatal编程技术网

Java 获取int数组中具有重复项的元素

Java 获取int数组中具有重复项的元素,java,arrays,Java,Arrays,这篇文章-“”,也是我在学校的书中的一个示例练习,但我想做的是在不排序的情况下获取具有重复项的元素 我所做的是首先删除数组的副本,以仅获取唯一的元素,然后将其与原始数组进行比较,并计算找到该元素的次数。但问题是它没有打印出正确的元素,而这些元素有重复项 int[] num = {7, 2, 6, 1, 4, 7, 4, 5, 4, 7, 7, 3, 1}; 正确的输出应该是:7,1,4 但它却输出:7,6,1 这是我的密码: //method for removing duplicates p

这篇文章-“”,也是我在学校的书中的一个示例练习,但我想做的是在不排序的情况下获取具有重复项的元素

我所做的是首先删除数组的副本,以仅获取唯一的元素,然后将其与原始数组进行比较,并计算找到该元素的次数。但问题是它没有打印出正确的元素,而这些元素有重复项

int[] num = {7, 2, 6, 1, 4, 7, 4, 5, 4, 7, 7, 3, 1};
正确的输出应该是:
7,1,4

但它却输出:
7,6,1

这是我的密码:

//method for removing duplicates
public static int[] removeDuplicates(int[] n) {
    int limit = n.length;

    for(int i = 0; i < limit; i++) {
        for(int j = i + 1; j < limit; j++) {
            if(n[i] == n[j]) {
                for(int k = j; k < limit - 1; k++) {
                    n[k] = n[k + 1];
                }
                limit--;
                j--;
            }
        }
    }

    int[] uniqueValues = new int[limit];
    for(int i = 0; i < uniqueValues.length; i++) {
        uniqueValues[i] = n[i];
    }

    return uniqueValues;
}

//method for getting elements that has duplicates
public static int[] getDuplicatedElements(int[] n) {
    int[] nCopy = n.clone();
    int[] u = removeDuplicates(nCopy);
    int count = 0;
    int limit = u.length;

    for(int i = 0; i < u.length; i++) {
        for(int j = 0; j < n.length; j++) {
            if(u[i] == n[j]) {
                count++;
            }
        }

        if(count == 1) {
            for(int k = i; k < limit - 1; k++) {
                u[k] = u[k + 1];
            }
            limit--;
        }
        count = 0;
    }

    int[] duplicated =  new int[limit];
    for(int i = 0; i < duplicated.length; i++) {
        duplicated[i] = u[i];
    }

    return duplicated;
}

//main
public static void main(String[] args) {
    int[] num = {7, 2, 6, 1, 4, 7, 4, 5, 4, 7, 7, 3, 1};

    //printing original values
    System.out.print(Arrays.toString(num));
    System.out.println();


    int[] a = getDuplicatedElements(num);
    System.out.print("Elements with Duplicates: " + Arrays.toString(a)); 
}
//删除重复项的方法
公共静态int[]移除的副本(int[]n){
int limit=n.长度;
对于(int i=0;i

我的代码有什么错误?请帮助,谢谢…

使用流时非常简单

int[] num = {7, 2, 6, 1, 4, 7, 4, 5, 4, 7, 7, 3, 1};
List<Integer> list = Arrays.stream(num).boxed().collect(Collectors.toList());
list.stream().filter(i -> Collections.frequency(list, i) > 1)
    .collect(Collectors.toSet()).forEach(System.out::println);
int[]num={7,2,6,1,4,7,4,5,4,7,7,3,1};
List List=Arrays.stream(num.boxed().collect(Collectors.toList());
list.stream().filter(i->Collections.frequency(list,i)>1)
.collect(Collectors.toSet()).forEach(System.out::println);
您有两个问题:

public static int[] getDuplicatedElements(int[] n) {
    int[] nCopy = n.clone();
    int[] u = removeDuplicates(nCopy);
    System.out.println ("unique " + Arrays.toString (u));
    int count = 0;
    int limit = u.length;

    for(int i = 0; i < limit; i++) { // you must use limit instead of u.length
                                     // in order for the loop to terminate
        for(int j = 0; j < n.length; j++) {
            if(u[i] == n[j]) {
                count++;
            }
        }

        if(count == 1) {
            for(int k = i; k < limit - 1; k++) {
                u[k] = u[k + 1];
            }
            limit--;
            i--; // you must decrement i after you find a unique element in u
                 // otherwise you'll be skipping elements in the u array
        }
        count = 0;
    }

    int[] duplicated =  new int[limit];
    for(int i = 0; i < duplicated.length; i++) {
        duplicated[i] = u[i];
    }

    return duplicated;
}

你调试过你的应用程序吗?每次我看到多个嵌套循环,我就开始发抖。@Robert我认为最好先对克隆的数组进行排序,然后再以线性方式抛出,如果一个变量与上一个变量相同,那么你就知道它是重复的。或者可能使用HashSet?@clangkill3r尽可能多地使用普通数组。。。这可能吗??
Elements with Duplicates: [7, 1, 4]