Java 如何让此列表在不打印两次值的情况下打印数组中的重复值
我的问题是,无论我尝试什么方法,该方法都会吐出重复数及其重复次数。所以不是输出Java 如何让此列表在不打印两次值的情况下打印数组中的重复值,java,arrays,loops,Java,Arrays,Loops,我的问题是,无论我尝试什么方法,该方法都会吐出重复数及其重复次数。所以不是输出 -12, 3, -12, 4, 1, 1, -12, 1, 1, 2, 3, 4, 2, 3, -12 The program output should be: N Count 4 2 3 3 2 2 1 4 -
-12, 3, -12, 4, 1, 1, -12, 1, 1, 2, 3, 4, 2, 3, -12
The program output should be:
N Count
4 2
3 3
2 2
1 4
-1 1
-12 4
它将输出:
"-12 4"
我也知道还有更先进、更高效的技术,但我们还没有学到很多。
提前谢谢
"-12 4"
"-12 4"
"-12 4"
"-12 4"
,另一个与您的代码匹配的解决方案
N Count
-12 4
1 4
2 2
3 3
4 2
public静态void printOrganizedList(int[]数组){
System.out.println(“N\t帐户”);
数组。排序(数组);
for(int i=0;i1)
System.out.println(数组[i]+“\t”+计数);
i+=计数;
}
}
如果输入数组的范围合理(例如,从-12到12,而不是从Integer.MIN\u值到Long.MAX\u值),则可以应用计数排序:
- 定义数组中的最小值和最大值
- 计算频率
- 并打印出频率大于1的数字:
int min=arr[0],max=arr[0];
对于(int i=1;imax)max=arr[i];
}
int[]freq=新int[max-min+1];
对于(int i=0;i1){
System.out.println((min+i)+“”+freq[min+i]);
}
}
这可以使用HashMap轻松实现。您可以创建一个Hashmap,它将元素保存为键,并将出现次数保留为值
int min = arr[0], max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) min = arr[i];
else if (arr[i] > max) max = arr[i];
}
int[] freq = new int[max - min + 1];
for (int i = 0; i < arr.length; i++) {
freq[min + i]++;
}
for (int i = 0; i < freq.length; i++) {
if (freq[min + i] > 1) {
System.out.println((min + i) + " " + freq[min + i]);
}
}
public静态void printOrganizedList(int[]数组){
系统输出打印项次(“N计数”);
HashMap countMap=新HashMap();
for(int i=0;i
另外,您所编写的程序的时间复杂度为O(N^2),对于大型程序来说,这可能是一个非常大的瓶颈
上述带有hashmap实现的程序只需花费您O(N)您是否可以使用hashmap在输入数组中存储数字的计数/频率?我们可以使用任何我相信的方法,但是我无法向他证明我使用hashmaps的合理性,因为我们还没有这样做。嵌套循环可能会得到改进:
for(int j=I;j
我对计算频率部分有一个问题。每当我只输入一次数字时,它将完全忽略该数字。所以,如果我输入{1,8,1,8,4},它会准确地打印出1和8,但根本不会打印出4,我该如何解决这个问题?
N Count
-12 4
1 4
2 2
3 3
4 2
public static void printOrganizedList(int[] array) {
System.out.println("N\tCount");
Arrays.sort(array);
for (int i = 0; i < array.length; i++) {
int count = 0;
// calc freq
for (int j = 0; j < array.length; j++) {
if (array[i] == array[j])
count++;
}
if (count > 1)
System.out.println(array[i] + "\t" + count);
i += count;
}
}
int min = arr[0], max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) min = arr[i];
else if (arr[i] > max) max = arr[i];
}
int[] freq = new int[max - min + 1];
for (int i = 0; i < arr.length; i++) {
freq[min + i]++;
}
for (int i = 0; i < freq.length; i++) {
if (freq[min + i] > 1) {
System.out.println((min + i) + " " + freq[min + i]);
}
}
public static void printOrganizedList(int[] array) {
System.out.println("N Count");
HashMap<Integer, Integer> countMap = new HashMap<>();
for (int i = 0; i < array.length; i++){
if (countMap.containsKey(array[i])){
int count = countMap.get(array[i]);
countMap.replace(array[i], count + 1);
}else{
countMap.put(array[i], 1);
}
}
Iterator iterator = countMap.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry mapElement = (Map.Entry) iterator.next();
int key = (int) mapElement.getKey();
int count = (int) mapElement.getValue();
System.out.println(key + " " + count);
}
}