[英]Counting occurrences of integers in an array in Java
注意:沒有映射,沒有排序
這是我的代碼:
public static void countArray(int[] n){
int[] m = new int[n.length]; //50 elements of integers between values of 10 & 20
int count = 0;
int sum = 0;
for ( int i = 0; i < n.length ; i++){
m[i] = n[i]; //make a copy of array 'n'
System.out.print(m[i]+" ");
}System.out.println();
for ( int j =0; j < n.length ; j++){
count =0;
for(int i = 0; i < n.length ; i++){
if (n[j]%m[i]==0 && n[j] == m[i])
count++;
}if ( n[j]%m[j] == 0)
System.out.println(m[j] + " occurs = " + count);
}
}
所以問題是:我在不同的行上得到重復的結果,如:“ 25發生= 5”。
我的想法:由於if ( n[j]%m[j] == 0)
所以我嘗試了if ( n[j]%m[j+1] == 0)
。 發生另一個問題,因為m[j]
將為m[50]
所以它崩潰了,但是給了我想要的結果。
我想要的結果:像這樣:沒有重復並且覆蓋了集合上的所有隨機整數
17 occurs = 3
23 occurs = 2
19 occurs = 3
15 occurs = 2
12 occurs = 2
試試這個:(對數組排序然后計算元素的出現)
public static void countArray(int[] n) {
int count = 0;
int i, j, t;
for (i = 0; i < n.length - 1; i++) // sort the array
{
for (j = i + 1; j < n.length; j++) {
if (n[i] > n[j]) {
t = n[i];
n[i] = n[j];
n[j] = t;
}
}
}
for (i = 0; i < n.length;)
{
for (j = i; j < n.length; j++) {
if (n[i] == n[j])
{
count++;
} else
break;
}
System.out.println(n[i] + " occurs " + count);
count = 0;
i = j;
}
}
經過一些調整,您的代碼應該可以工作:
public static void countArray(int[] n){
boolean [] alreadyCounted = new boolean[n.length];
for (int i = 0; i < n.length ; i++){
int count = 0;
if (alreadyCounted[i]) {
// skip this one, already counted
continue;
}
for(int j = 0; j < n.length ; j++){
if (n[i] == n[j]) {
// mark as already counted
alreadyCounted[j] = true;
count++;
}
}
System.out.println(n[i] + " occurs = " + count);
}
}
您肯定可以使用具有更好代碼的相同邏輯,而我只是嘗試遵循原始的“編碼樣式”。
這是O(n ^ 2)解(讀為“非常慢” )。
如果可以使用排序,則可以在O(n log(n))中進行-這是fast 。
使用映射,您可以在O(n)中完成它- 速度非常快 ;
如果您利用輸入限制,則可能會丟失嵌套循環:
public static void main(String[] args)
{
//6 elements of integers between values of 10 & 20
int[] countMe = { 10, 10, 20, 10, 20, 15 };
countArray(countMe);
}
/** Count integers between values of 10 & 20 (inclusive) */
public static void countArray(int[] input)
{
final int LOWEST = 10;
final int HIGHEST = 20;
//Will allow indexes from 0 to 20 but only using 10 to 20
int[] count = new int[HIGHEST + 1];
for(int i = 0; i < input.length; i++)
{
//Complain properly if given bad input
if (input[i] < LOWEST || HIGHEST < input[i])
{
throw new IllegalArgumentException("All integers must be between " +
LOWEST + " and " + HIGHEST + ", inclusive");
}
//count
int numberFound = input[i];
count[numberFound] += 1;
}
for(int i = LOWEST; i <= HIGHEST; i++)
{
if (count[i] != 0)
{
System.out.println(i + " occurs = " + count[i]);
}
}
}
這是一種不錯的,高效的方法,比這里發布的其他解決方案更有效。 該數組以O(n)時間運行,其中數組的長度為n。 假設您有一些數字MAX_VAL
,它表示您可能在數組中找到的最大值,並且最小值為0。在您的注釋中,建議MAX_VAL==20
。
public static void countOccurrences(int[] arr) {
int[] counts = new int[MAX_VAL+1];
//first we work out the count for each one
for (int i: arr)
counts[i]++;
//now we print the results
for (int i: arr)
if (counts[i]>0) {
System.out.println(i+" occurs "+counts[i]+" times");
//now set this count to zero so we won't get duplicates
counts[i]=0;
}
}
它首先遍歷數組,每次找到一個元素時都會增加相關計數器。 然后返回,並打印出每個計數。 但是,至關重要的是,每次打印整數的計數時,都會將其計數重置為0,這樣就不會再次打印該計數。
如果您不喜歡for (int i: arr)
樣式,則完全相同:
public static void countOccurrences(int[] arr) {
int[] counts = new int[MAX_VAL+1];
//first we work out the count for each one
for (int i=0; i<arr.length; i++)
counts[arr[i]]++;
//now we print the results
for (int i=0; i<arr.length; i++)
if (counts[arr[i]]>0) {
System.out.println(arr[i]+" occurs "+counts[arr[i]]+" times");
//now set this count to zero so we won't get duplicates
counts[arr[i]]=0;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.