如何更改代码,以便只能从数组中获得一个数字和重复的次数?
我尝试了经典方法,但是它只显示了“ 2次重复2次” x2行,“ 0次重复3次” x3行等,当我只希望一次时“ 2次重复2次; 0次重复3次”等等
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] array = {2, 0, -12, 0, 23, 45, -4, -5, 2, 23, 0, 9, -7};
Arrays.sort(array);
for(int i=0;i<array.length;i++){
int count = 0;
for(int j=i+1;j<array.length;j++){
if(array[i]==array[j] && i != j){
count = count + 1;
System.out.println("elements" + array[i] + " repeats" + count + " times);
}
}
}
}
}
答案 0 :(得分:2)
由于数组已排序,因此只需要一个循环:
public static void main(String[] args) {
int[] array = {2, 0, -12, 0, 23, 45, -4, -5, 2, 23, 0, 9, -7};
Arrays.sort(array);
int index = 0;
int counter = 1;
while (index < array.length - 1) {
if (array[index] == array[index + 1]) {
counter++;
} else {
if (counter > 1) {
System.out.println("element " + array[index] + " repeats " + counter + " times");
}
counter = 1;
}
index++;
}
if (counter > 1) {
System.out.println("element " + array[index] + " repeats " + counter + " times");
}
}
它将每个元素与下一个元素进行比较。如果它们相等,则计数器递增;如果大于1,则计数器递增;这意味着存在重复项,并打印行:
"element " + array[index] + " repeats " + counter + " times"
如果不大于1,则索引递增,计数器重置为1。
与for循环相同:
for (index = 0; index < array.length - 1; index++) {
if (array[index] == array[index + 1]) {
counter++;
} else {
if (counter > 1) {
System.out.println("element " + array[index] + " repeats " + counter + " times");
}
counter = 1;
}
}
答案 1 :(得分:0)
这是做到这一点的一种方法。
int[] array = { 2, 0, -12, 0, 23, 45, -4, -5, 2, 23, 0, 9, -7
};
Arrays.sort(array);
int count = 1;
int v = array[0];
for (int i = 1;i < array.length;) {
while (i < array.length && v == array[i++]) {
count++;
}
if (count > 1) {
System.out.println(v + " occurs " + count + " times.");
}
v = array[i - 1];
count = 1;
}
这里有一些不需要排序的其他方式。
public static void method2() {
int[] array = { 2, 0, -12, 0, 23, 45, -4, -5, 2, 23, 0, 9, -7
};
Map<Integer, Integer> map = new HashMap<>();
for (int i : array) {
// If the map contains the number, bump the count
if (map.containsKey(i)) {
int count = map.get(i)+1; // increment count
map.put(i,count); // put it back
} else {
map.put(i, 1); // intialize count to 1
}
}
// Now go thru the map and display only the numbers when the value is > 1
for (Map.Entry<?,Integer> e : map.entrySet()) {
if (e.getValue() > 1) {
System.out.println(e.getKey() + " occurs " + e.getValue() + "
times.");
}
}
}
下一个方法是利用从Java 8开始的Stream功能。
public static void method3() {
int[] array = { 2, 0, -12, 0, 23, 45, -4, -5, 2, 23, 0, 9, -7
};
Arrays.stream(array)
// Convert the integers to an Integer object.
.boxed()
// group the values into a Map<Integer, Integer> where the key is
// the number
// and the value is the count.
.collect(Collectors.groupingBy(k -> k, Collectors.counting()))
// grab the entry set of that map
.entrySet()
// convert it to a stream
.stream()
// filter for entrysets where the value > 1
.filter(e -> e.getValue() > 1)
// and forEach entry set that passes the filter, print out the
// number and its count.
.forEach(e -> System.out.println(
e.getKey() + " occurs " + e.getValue() + " times."));
}
答案 2 :(得分:0)
如果您使用Java 8或更高版本,则可以按数字作为键并将其显示为值的次数对映射中的元素进行分组。之后,您可以过滤并仅显示出现多次的元素。
import java.util.Arrays;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
Integer[] array = {2, 0, -12, 0, 23, 45, -4, -5, 2, 23, 0, 9, -7};
Arrays.stream(array)
.collect(Collectors.groupingBy(s -> s))
.entrySet()
.stream()
.filter(e -> e.getValue().size() > 1)
.forEach(e -> System.out.println("elements " + e.getKey() + " repeats " + e.getValue().size() + " times"));
}
}