2011-08-21 33 views

回答

3

- 只是使用其中包含數組的索引值作爲鍵和其出現的次數作爲值的HashMap中。

- 在遍歷for循環時更新HashMap,方法是檢查當前索引是否已存在於HashMap中。然後,如果它在哈希映射中發現了兩倍,並查看它已經發生了多少次,並將其重新放回到HashMap中,並再發生一次。

- 我是用Java做的,因爲這就是它看起來像你使用的。還有一個好處是,時間複雜度爲O(n),這是您可能獲得的最佳情景,因爲您必須至少訪問一次元素。

- 所以如果你有這樣的雙打數組:{1,2,3,1,1,1,5,5,5,7,7,7,7,7,7,7,7 ,7} 然後,在末尾哈希映射將如下所示:{1-> 4,2-> 1,3-> 1,3,5,7,7> 9} 意思是「1發生了4次,2發生1次.... 7發生9次」等的

public static double mode(double [] arr) 
    { 
     HashMap arrayVals = new HashMap(); 
     int maxOccurences = 1; 
     double mode = arr[0]; 

     for(int i = 0; i<arr.length; i++) 
     { 
      double currentIndexVal = arr[i]; 
      if(arrayVals.containsKey(currentIndexVal)){ 
       int currentOccurencesNum = (Integer) arrayVals.get(currentIndexVal); 
       currentOccurencesNum++; 
       arrayVals.put(currentIndexVal, currentOccurencesNum); 
       if(currentOccurencesNum >= maxOccurences) 
       { 
        mode = currentIndexVal; 
        maxOccurences = currentOccurencesNum; 
       } 
      } 
      else{ 
       arrayVals.put(arr[i], 1); 
      } 
     } 


     return mode; 
    } 
+0

感謝您的回覆。我能夠使用for循環找出它。 – jlss4e

+0

public static double mode(double [] arr) \t { \t \t double loc = 0,val = -1; \t \t \t 爲\t(INT I = 1;我 LOC) \t \t \t { \t \t \t \t VAL = ARR [I]; \t \t \t \t loc = count; \t \t \t} \t \t} \t \t \t {\t \t \t \t \t } \t \t \t \t \t \t 返回\t VAL; – jlss4e

+0

是的jlss4e你的確可以工作,但它必須將每個元素與每個其他元素進行比較,因爲嵌套for循環。所以它必須運行n *(n-1)次。所以如果有100個輸入,它將不得不跑100 * 99次。如果你不擔心性能,它似乎是完美的 - - ) – Ray

4

首先我陣列排序順序,然後我算一個號碼的出現。沒有hashmaps只用於循環和if語句。

我的代碼:

static int Mode(int[] n){ 
    int t = 0; 
    for(int i=0; i<n.length; i++){ 
     for(int j=1; j<n.length-i; j++){ 
      if(n[j-1] > n[j]){ 
       t = n[j-1]; 
       n[j-1] = n[j]; 
       n[j] = t; 
      } 
     } 
    } 

    int mode = n[0]; 
    int temp = 1; 
    int temp2 = 1; 
    for(int i=1;i<n.length;i++){ 
     if(n[i-1] == n[i]){ 
      temp++; 
     } 
     else { 
      temp = 1; 
     } 
     if(temp >= temp2){ 
      mode = n[i]; 
      temp2 = temp; 
     } 
    } 
    return mode; 
} 
+1

只需要將double []更改爲double [],如果需要雙數組 – Deyan

1

此代碼是不使用包含HashMap不同的方式。這個在java中創建的方法將一個數組作爲參數,並在該方法內創建另一個名爲「numberCount」的數組。該數組「numberCount」將其索引設置爲數組中的值。包含傳遞數組中的值的「numberCount」的索引將向「numberCount」(「++ numberCount [array [i]]」)的值加1,然後將轉到數組中的下一個值(重複直到數組的末尾)。然後創建另一個for循環來遍歷「numberCount」中的數組的每個值,其中索引具有最高值/計數的數據將被存儲並返回爲「max」。這種方法將不得不經歷一些困難的改變來使用雙數組。但似乎很適合int數組。

public static int findMostFrequentValue(int[] array) { 
    int i; 
    int[] numberCount = new int[100]; 
    for (i = 0; i < array.length; i++)++numberCount[array[i]]; 
    int max = 0; 
    int j; 

    for (j = 0; j < numberCount.length; j++) { 
     if (numberCount[j] > max) max = j; 
    } 
    return max; 
} 
0

您應該檢查陣列中每個元素的發生次數。你可以通過比較數組的每個元素與自己和其他人通過2個內部for循環來完成。請記住,如果數組未被排序並且包含多於一個模態值(因此重複發生的次數),則將返回第一個數組。首先通過 Arrays.sort(array)排列陣列可能是明智之舉,這樣您就可以選擇最小或最大的模態值。

public static int modeOfArray(int[] array){ 
    int mode;  
    int maxOccurance = 0; 

    for(int i=0; i<array.length; i++){ 
     int occuranceOfThisValue = 0; 
     for(int j=0; j<array.length; j++){ 
      if(array[i] == array[j]) 
       occuranceOfThisValue++; 
     } 

     if(occuranceOfThisValue > maxOccurance){ 
      maxOccurance = occuranceOfThisValue; 
      mode = array[i]; 
     } 
    } 
    return mode; 
}