Java 整数数组中的重复值数

Java 整数数组中的重复值数,java,arrays,Java,Arrays,我需要一个函数或方法来找到在一个值中重复的值的数量。我不是在寻找重复的数字,或者重复的次数,而是寻找一般重复的数字的数量。这听起来可能令人困惑,因此下面是一个示例: int[]anArray={ 1, 2, 3, 4, 3, 1, 7, 2, 9, 1 }; 数字1有多个实例,2和3也有多个实例 因此,我的输出应该是“有3个连续值” 这就是我到目前为止所做的: intx; int i=0; int[]数组=新的int[10]; 做{ x=input.nextInt(); 数组[i]=x; i

我需要一个函数或方法来找到在一个值中重复的值的数量。我不是在寻找重复的数字,或者重复的次数,而是寻找一般重复的数字的数量。这听起来可能令人困惑,因此下面是一个示例:

int[]anArray={
1, 2, 3,
4, 3, 1, 
7, 2, 9, 1
};
数字1有多个实例,2和3也有多个实例
因此,我的输出应该是“有3个连续值”
这就是我到目前为止所做的:

intx;
int i=0;
int[]数组=新的int[10];
做{
x=input.nextInt();
数组[i]=x;
i++;
}而(x!=0);

在循环之外,我需要能够输出重复值的数量。这是while循环练习,因此我不允许使用for循环。

您可以使用以下伪代码:

  • 给定一个数字数组
  • 按数字将所有值分组
  • 重复过滤所有值
  • 打印所有重复的数字
具体实施如下:

        int[] anArray = { 1, 2, 3, 4, 3, 1, 7, 2, 9, 1 };
        Arrays.stream(anArray).boxed()
            .collect(Collectors.groupingBy(Integer::intValue))
            .values().stream().filter(l->l.size()>1)
            .forEach(numbers -> System.out.println(numbers.get(0)));
输出将是:

1
2
3   

您可以使用以下伪代码:

  • 给定一个数字数组
  • 按数字将所有值分组
  • 重复过滤所有值
  • 打印所有重复的数字
具体实施如下:

        int[] anArray = { 1, 2, 3, 4, 3, 1, 7, 2, 9, 1 };
        Arrays.stream(anArray).boxed()
            .collect(Collectors.groupingBy(Integer::intValue))
            .values().stream().filter(l->l.size()>1)
            .forEach(numbers -> System.out.println(numbers.get(0)));
输出将是:

1
2
3   

如果必须使用while循环,则可以尝试使用Arrays.sort函数的以下简单算法:

int getRepeatedElements(int[] array){
    //Sort the numbers in ascending order
    java.util.Arrays.sort(array);

    //Strategy: Store the first occurrence of each number and then move to the next element to see if it is repeated
    int currentElement = array[0];
    int repeatedCount = 0;
    int i = 1;
    boolean alreadyCounted = false;

    while(i < array.length){
        if(currentElement == array[i]){
            //Found a repeated element!
            if(!alreadyCounted){
                repeatedCount++;
                alreadyCounted = true;
            }
        }
        else{
            //New element found
            currentElement = array[i];
            alreadyCounted = false;
        }
        i++; //Move to the next element
    }
    return repeatedCount;
}
int getRepeatedElements(int[]数组){
//将数字按升序排序
java.util.Arrays.sort(数组);
//策略:存储每个数字的第一次出现,然后移动到下一个元素,查看是否重复
int currentElement=数组[0];
int repeatedCount=0;
int i=1;
布尔值alreadyCounted=false;
while(i
如果必须使用while循环,则可以尝试使用Arrays.sort函数的以下简单算法:

int getRepeatedElements(int[] array){
    //Sort the numbers in ascending order
    java.util.Arrays.sort(array);

    //Strategy: Store the first occurrence of each number and then move to the next element to see if it is repeated
    int currentElement = array[0];
    int repeatedCount = 0;
    int i = 1;
    boolean alreadyCounted = false;

    while(i < array.length){
        if(currentElement == array[i]){
            //Found a repeated element!
            if(!alreadyCounted){
                repeatedCount++;
                alreadyCounted = true;
            }
        }
        else{
            //New element found
            currentElement = array[i];
            alreadyCounted = false;
        }
        i++; //Move to the next element
    }
    return repeatedCount;
}
int getRepeatedElements(int[]数组){
//将数字按升序排序
java.util.Arrays.sort(数组);
//策略:存储每个数字的第一次出现,然后移动到下一个元素,查看是否重复
int currentElement=数组[0];
int repeatedCount=0;
int i=1;
布尔值alreadyCounted=false;
while(i
试试这个:

import java.util.*;
公共班机{
公共静态void main(字符串[]args){
int[]anArray={1,2,3,4,3,1,7,2,9,1,1,2};
java.util.Arrays.sort(anArray);
System.out.println(Arrays.toString(anArray));
int countedElement=anArray[0];//已选中元素
整数计数=0;
for(int i=1;i
试试这个:

import java.util.*;
公共班机{
公共静态void main(字符串[]args){
int[]anArray={1,2,3,4,3,1,7,2,9,1,1,2};
java.util.Arrays.sort(anArray);
System.out.println(Arrays.toString(anArray));
int countedElement=anArray[0];//已选中元素
整数计数=0;
for(int i=1;i
我不认为“连续”意味着你认为它意味着什么。如果你可以使用
for
循环解决这个问题,你可以很容易地将它转换成
while
循环。如果你有一个数字的3,会发生什么?这算不算一个案例?这能回答你的问题吗?有很多方法可以做到这一点。你能说出哪些数组元素可以有值的范围吗?我不认为“连续”意味着你认为它的意思。如果你能使用
for
循环解决这个问题,你可以很容易地将它转换成
while
循环。如果你有一个数字的3,会发生什么?这算不算一个案例?这能回答你的问题吗?有很多方法可以做到这一点。您能告诉数组元素可以具有的值的范围吗?请注意,此函数的副作用是重新排列作为参数传递的数组。如果不需要这样做,则可以复制原始数组,然后将该副本作为参数传递。请注意,此函数的副作用是重新排列作为参数传递的数组。如果不需要这样做,则可以创建原始数组的副本,然后将该副本作为参数传递。