检测原始Java数组中的重复值
我想检测Java数组中的重复值。例如:检测原始Java数组中的重复值,java,arrays,duplicates,primitive,Java,Arrays,Duplicates,Primitive,我想检测Java数组中的重复值。例如: int[] array = { 3, 3, 3, 1, 5, 8, 11, 4, 5 }; 如何获取特定的重复条目及其出现次数?为第一步分配一个计数器,然后您可以将它们关联到另一个数组,将每个数字分配到索引,然后如果您的数字重复,则增加计数器…公共类重复{ public class Duplicate { public static void main(String[] arg) { int[] array = {1, 3, 5
int[] array = { 3, 3, 3, 1, 5, 8, 11, 4, 5 };
如何获取特定的重复条目及其出现次数?为第一步分配一个计数器,然后您可以将它们关联到另一个数组,将每个数字分配到索引,然后如果您的数字重复,则增加计数器…
公共类重复{
public class Duplicate {
public static void main(String[] arg) {
int[] array = {1, 3, 5, 6, 2, 3, 6, 4, 3, 2, 1, 6, 3};
displayDuplicate(array);
}
static void displayDuplicate(int[] ar) {
boolean[] done = new boolean[ar.length];
for(int i = 0; i < ar.length; i++) {
if(done[i])
continue;
int nb = 0;
for(int j = i; j < ar.length; j++) {
if(done[j])
continue;
if(ar[j] == ar[i]) {
done[j] = true;
nb++;
}
}
System.out.println(ar[i] + " occurs " + nb + " times");
}
}
}
公共静态void main(字符串[]arg){
int[]数组={1,3,5,6,2,3,6,4,3,2,1,6,3};
显示重复(数组);
}
静态无效显示重复(int[]ar){
布尔值[]完成=新布尔值[ar.length];
for(int i=0;i
我将有一个映射
,其中第一个整数是数组中出现的数字的值
,第二个整数是计数
(出现次数)
- 在循环中运行
数组。长度
- 对于数组中的每个项目,执行
。如果映射中存在一个数字,则增加该数字(类似于map.containsKey(数组[i])
。否则,在映射中创建一个新条目(例如map.put(array[i],map.get(array[i])+1)
)map.put(array[i],1)
- 最后,遍历映射并检索值大于1的所有键
int[] counts = new int[max_value + 1];
for (int n: array) {
counts[n]++;
}
如果源数组包含未知或太大的范围,则可以创建映射
,并在其中计数:
Map<Integer,Integer> counts = new HashMap<Integer,Integer>();
for (Integer n: array) {
if (counts.containsKey(n)) {
counts.put(n, counts.get(n) + 1);
} else {
counts.put(n, 1);
}
}
Map counts=newhashmap();
for(整数n:数组){
如果(计算容器(n)){
counts.put(n,counts.get(n)+1);
}否则{
计数。放置(n,1);
}
}
注意:在没有JVM帮助的情况下键入上面的内容,就可以消除键入错误
作为读者的练习:-)对数组进行排序,然后扫描它或
数组。binarySearch
+向任意方向扫描。由于分配更少且没有包装,这可能会更快,尤其是在较大的数组上。看起来像是一项名为multiset
的数据结构作业
Multiset<Integer> mp = HashMultiset.create();
mp.addAll(Arrays.asList(new Integer[] { 3, 3, 3, 1, 5, 8, 11, 4, 5 }));
这将输出:
1 is contained 1 times.
3 is contained 3 times.
4 is contained 1 times.
5 is contained 2 times.
8 is contained 1 times.
11 is contained 1 times.
import java.util.HashMap;
导入java.util.Iterator;
导入java.util.Map;
公共类DuplicatedValuesInArray
{
公共静态void main(字符串args[]){
int[]数组={3,3,3,1,5,8,11,4,5};
Map Map=newhashmap();
对于(inti=0;iJava8),解决方案:
1.当键为数组的值且值为计数器时创建映射。
2.检查Map是否包含钥匙增加计数器或添加新设置。
私有静态void CalculatedBlicateValues(int[]数组){
//键是数组的值,值是计数器
Map Map=newhashmap();
for(整型元素:数组){
if(映射容器(元素)){
map.put(元素,map.get(元素)+1);//如果包含,则增加计数器
}否则
地图放置(元素,1);
}
图.forEach((k,v)->{
如果(v>1)
System.out.println(“元素”+k+“复制”+v+“次数”);
});
}
您可以使用Collectors.frecuency()和Collectors.groupingBy
我就是这样做的,希望这能对你有所帮助
Map<Object,Long> finalValues = new HashMap<Object, Long>();
finalValues = Arrays.asList(new Integer[] {3, 3, 3, 1, 5, 8, 11, 4, 5 })
.stream()
.collect(Collectors.groupingBy(e -> e, Collectors.counting()));
//Check the output
finalValues.entrySet().forEach(entry -> {
System.out.println("number: " + entry.getKey() + "| Times: "+ entry.getValue());
});
您甚至可以使用frecuency删除所有不重复的数字:
Map finalValues=newhashmap()
你在寻找什么样的价值观?3还是5?我试图找到3和5以及它们是如何出现的。请不要用代码回答一个明显的家庭作业问题。给出提示或其他任何东西——除了为他做家庭作业。是的,看看问题的性质,确定这是一个家庭作业问题,他们不会总是这样我只是指出了这一点,因为人们一般都乐于助人,有时不真正考虑问题的本质。“YANIS IS HistoFakis,不。这是一个打印错误。把错误固定在我的帖子上。这个答案不跟踪重复值出现的次数。你还可以对代码做些解释吗?
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class DuplicatedValuesInArray
{
public static void main(String args[]) {
int[] array = { 3, 3, 3, 1, 5, 8, 11, 4, 5 };
Map<Integer, Integer> map= new HashMap<Integer, Integer>();
for(int i=0;i<array.length;i++) {
if(map.containsKey(array[i]))
map.put(array[i],map.get(array[i]) + 1);
else
map.put(array[i], 1);
}
for (Integer i : map.keySet()) {
System.out.println(i + " is contained " + map.get(i) + " times.");
}
}
}
Java 8, the solution:
1. Create Map when the Key is the Value of Array and Value is counter.
2. Check if Map contains the Key increase counter or add a new set.
private static void calculateDublicateValues(int[] array) {
//key is value of array, value is counter
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Integer element : array) {
if (map.containsKey(element)) {
map.put(element, map.get(element) + 1); // increase counter if contains
} else
map.put(element, 1);
}
map.forEach((k, v) -> {
if (v > 1)
System.out.println("The element " + k + " duplicated " + v + " times");
});
}
Map<Object,Long> finalValues = new HashMap<Object, Long>();
finalValues = Arrays.asList(new Integer[] {3, 3, 3, 1, 5, 8, 11, 4, 5 })
.stream()
.collect(Collectors.groupingBy(e -> e, Collectors.counting()));
//Check the output
finalValues.entrySet().forEach(entry -> {
System.out.println("number: " + entry.getKey() + "| Times: "+ entry.getValue());
});
number: 1| Times: 1
number: 3| Times: 3
number: 4| Times: 1
number: 5| Times: 2
number: 8| Times: 1
number: 11| Times: 1
List<Integer> numbers = Arrays.asList(new Integer[]{1,2,1,3,4,4});
finalValues = numbers
.stream()
.filter(x-> Collections.frequency(numbers, x) > 1)
.collect(Collectors.groupingBy(e -> e, Collectors.counting()));
//Check the output
finalValues.entrySet().forEach(entry -> {
System.out.println("number: " + entry.getKey() + "| Times: "+ entry.getValue());
});
number: 1| Times: 2
number: 4| Times: 2