计算数组中一个元素的重复次数-JAVA面向只懂循环的初学者
我记得5年前有一个答案,但把这个问题放在上下文中让我作为一名第一年的Java学习者很难理解 我想找出元素在数组中重复自身的次数。 例如 如果我有数组{1,2,3,4,5,6,7,7,7,8} 在事先不知道数组的情况下,如何知道其中一个元素是否重复 下面是:计算数组中一个元素的重复次数-JAVA面向只懂循环的初学者,java,arrays,Java,Arrays,我记得5年前有一个答案,但把这个问题放在上下文中让我作为一名第一年的Java学习者很难理解 我想找出元素在数组中重复自身的次数。 例如 如果我有数组{1,2,3,4,5,6,7,7,7,8} 在事先不知道数组的情况下,如何知道其中一个元素是否重复 下面是: public class FindRepeats { public static void main(String[] args) { int[] array = {1,2,3,4,5,6,7,7,7,8};
public class FindRepeats {
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6,7,7,7,8};
System.out.println(findCount(array));
}
public static String findCount(int[] arr) {
int posIndex = 0;
int count = 1;
for(int i=0; i<arr.length; i++) {
for(int j=i+1; j<arr.length; j++) {
//This for-loop will allow comparison within the array with all other values of the array.
if(arr[i]==arr[j]) {
//Because we know the first element (i) will always be the same as the first element (j), we start j at index 1.
posIndex = i;
count += 1;
}
}
}
return "Repeated elements are:\n" + arr[0] + " : " + count;
}
公共类查找重复{
公共静态void main(字符串[]args){
int[]数组={1,2,3,4,5,6,7,7,8};
System.out.println(findCount(数组));
}
公共静态字符串findCount(int[]arr){
int posIndex=0;
整数计数=1;
对于(int i=0;ifor(int i=0;i这将生成事件图:
Map<Integer, Long> map =
list.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
Map=
list.stream().collect(Collectors.groupingBy(Function.identity(),Collectors.counting());
此示例生成一个包含多次出现的数字的集合
Integer[] array = {1,2,3,4,5,6,7,7,7,8};
List<Integer> list = new ArrayList<Integer>(Arrays.asList(array));
list.stream().filter(i -> Collections.frequency(list, i) >1)
.collect(Collectors.toSet()).forEach(System.out::println);
Integer[]数组={1,2,3,4,5,6,7,7,8};
List List=newarraylist(Arrays.asList(array));
list.stream().filter(i->Collections.frequency(list,i)>1)
.collect(Collectors.toSet()).forEach(System.out::println);
让我们享受一下流API的乐趣(请注意,我对这个API不太熟悉,所以它可能并不完美):
这将迭代数组,计算发生次数并返回一个Map
Map<Integer, Long> map = Arrays.stream(array)
.boxed() //to work with a `Stream<Integer>`
.collect( //get a `Map`
Collectors.groupingBy( //Grouping every value in the `Stream`
Function.identity(),
//HashMap<Integer, Long>::new, //Not needed, `groupingBy` define a method with the same default collection.
Collectors.counting() //by counting the number of occurrence
)
);
map.forEach((o,l) -> { //then iterate the map
if(l > 1) { //if the counter is bigger than one
System.out.println(o); //print the value
}
});
PS:不幸的是,我没有找到一个更干净的解决方案来打印结果,我不能像过滤列表那样过滤映射
…如果有人有任何想法,我很乐意将其放在这里。我总是可以迭代映射
的条目
…但我觉得这是一种过度杀伤力(所有的解决方案都是一种过度杀伤力…)
将输出:
1=2
3=2
7=2
publicstaticvoidmain(字符串[]args){
int[]数组={1,2,3,4,5,6,7,7,8};
int[]结果=findCount(数组);
对于(int i=0;i请显示一些您尝试过的代码可能重复的代码有没有办法在不使用字典的情况下解决它?我不确定它是什么。数组总是被排序的还是您也必须处理未排序的数据?重复的条目是否总是同时出现?我希望它是总的重复次数。(需要同时为未排序和已排序的对象工作)您的第一个代码段中的id
是什么?不要尝试获取重复值,您将只获取最后一个。id
是任何用于存储重复编号的temp int变量。此程序似乎只返回数组中的最后一个元素。如果我想要每个元素的重复次数,该怎么办?@Pete请再次参考答案,我已经编辑了它,我希望它能解决您的问题。@PrashantBahuguna请编辑代码。就像前两部分看起来很粗糙,不起作用。用坦克壳杀死蚊子,我喜欢+1。@Stavm我完全同意,但我正在练习这类问题;)我真的很喜欢你用了一种有效的方法“用水箱灭蚊!”呵呵。但我正在寻找一个干净简单的方法解决方案,因为我还不了解高级java概念。我不知道函数。identity
!频率解决方案很有趣。你能解释一下代码吗?太糟糕了,它需要使用集合
,看起来使用起来很昂贵。函数.identity()
表示当前变量本身。frequency
方法返回某个元素在集合中出现的次数。因此,将其与>1
进行比较,您就有了过滤器。虽然我不知道它是否昂贵。。在回答中;)我得到了代码,但有一些注释会更正确。
Integer[] array = {1,2,3,4,5,6,7,7,7,8};
List<Integer> list = new ArrayList<Integer>(Arrays.asList(array));
list.stream().filter(i -> Collections.frequency(list, i) >1)
.collect(Collectors.toSet()).forEach(System.out::println);
Map<Integer, Long> map = Arrays.stream(array)
.boxed() //to work with a `Stream<Integer>`
.collect( //get a `Map`
Collectors.groupingBy( //Grouping every value in the `Stream`
Function.identity(),
//HashMap<Integer, Long>::new, //Not needed, `groupingBy` define a method with the same default collection.
Collectors.counting() //by counting the number of occurrence
)
);
map.forEach((o,l) -> { //then iterate the map
if(l > 1) { //if the counter is bigger than one
System.out.println(o); //print the value
}
});
{ 1, 2, 3, 3, 4, 5, 1, 6, 7, 7, 8 } > 1, 3, 7
map.entrySet()
.stream()
.filter(e -> e.getValue() > 1)
.forEach(System.out::println);
1=2
3=2
7=2
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6,7,7,7,8};
int[] result= findCount(array);
for (int i=0; i<result.length; l++)
System.out.print(result[i]+" ");
System.out.println();
}
public static int[] findCount(int[] arr) {
int[] result = new int[arr.length];
for (int i=0; i<arr.length; i++){
for (int j=0; j<arr.length; j++) {
if (arr[i]==arr[j])
result[i]++;
}
}
return result;
}