2012-05-05 27 views
2

假設我有一個整數陣列是這樣的: {5,3,5,4,2}如何獲得數組中最常見的字符?

和我有,它返回最常見的字符

public int highestnumber(String[] num) { 

     int current_number = Integer.parseInt(num[0]); 
     int counter = 0; 
     for (int i = 1; i < num.length; ++i) { 
      if (current_number == Integer.parseInt(num[i])) { 
       ++counter; 

      } else if (counter == 0) { 
       current_number = Integer.parseInt(num[i]); 
       ++counter; 

      } else { 
       --counter; 

      } 
     } 

     return current_number; 
    } 

的方法,但如果我有多個常見的字符然後我需要得到最接近一(1)的數字,就像我有這樣一個數組: {5,5,4,4,2};

那麼該方法應該返回4,我該怎麼做呢?

+0

如果你的陣列是這樣{5,4,5,5,4,2,2,5}然後號碼,你想要什麼?還有一個問題你爲什麼要傳遞字符串數組?它可能是整數數組?你想要普通人物還是普通人物? – vnshetty

+0

在這種情況下,我得到了5,並且它是正確的,但是隻有當最常見的字符是多個時,我才面臨問題。就像如果我有5個三次,4個三次陣列中的話,我應該得到4,因爲它是最接近1。而對於我的編碼我必須通過字符串數組,它不是一個大問題 – Reyjohn

+3

這無關與Android 。添加使用代碼的上下文並不總是相關的。 – keyser

回答

2

按我理解你的問題,

你必須做什麼,

1. Create ArrayList from your int[] 
2. Use HashMap for find duplicates, which one is unique 
3. Sort it as Ascending order, 
4. First element is what you want.. 

編輯:回答你的問題

int[] arr = {5, 4, 5, 4, 2}; 
ArrayList<Integer> resultArray = new ArrayList<Integer>(); 
Set<Integer> set = new HashSet<Integer>(); 

for (int i = 0; i < arr.length; i++) 
    { 
    if (set.contains(arr[i])) 
    { 
    System.out.println("Duplicate value found at index: " + i); 
    System.out.println("Duplicate value: " + arr[i]); 
     resultArray.add(arr[i]); 
    } 
    else 
    { 
    set.add(arr[i]); 
    } 
    } 
Collections.sort(resultArray); 

for (int i = 0; i < resultArray.size(); i++) 
{ 
Log.e("Duplicate Values:", resultArray.get(i) + ""); 
} 

您的需要是,

int values = resultArray.get(0); 
+0

我想你沒有正確地得到我的問題。我從我自己的方法中獲得最常見的角色,並且我對它很滿意。但是當我的答案是多個時,如果我有兩個數字出現在數組中,那麼我面臨問題,那麼它應該返回最小數字。 – Reyjohn

+0

在ArrayList中添加所有最常見的字符,並按升序排序。 – user370305

+0

我在努力,我會讓你知道嗎? – Reyjohn

2

對數組進行排序然後計算值的運行。

1

快速的方法。 爲每個數字創建一個計數器int數組一個元素。遍歷數組一次,併爲每個數字增加相應的計數器數組。設置最高數字爲第一個計數器元素,然後通過並將最大數字更改爲當前元素,只要其大於最高數字,則返回最高數字。

public int highestNumber(String[] num){ 
    int[] count = new int[10]; 
    int highest_number = 0; 
    int highest_value = 0; 

    for(int i = 0; i < num.length; i++) 
     count[Integer.parseInt(num[i])]++;; 

    for(int i = 0; i < count.length; i++) 
     if(count[i] > highest_value){ 
      highest_number = i; 
      highest_value = count[i]; 
     } 

    return highest_number; 
} 

慢10倍,但沒有其他陣列。 爲數字創建三個整數,用於計數兩個。每次對每個int進行一次數組讀取,並在每次顯示時遞增當前計數,如果大於最大計數,則設置爲最高計數並將最大數設置爲當前計數。返回最高號碼。

public int highestNumber(String[] num){ 
    int highest_number = 0; 
    int highest_value = 0; 
    int current_value = 0; 

    for(int i = 0; i < 10; i++){ 
     for(int j = 0; j < num.length; j++) 
      if(i == Integer.parseInt(num[j])) 
       current_value++; 

     if(current_value > highest_value){ 
      highest_value = current_value; 
      highest_number = i; 
     } 

     current_value = 0; 
    } 

    return highest_number; 
} 

第一個顯然要快得多,但如果因爲某種原因你不想要另一個數組,第二個數組也可以工作。

1

你也可以試試這個:

import java.util.TreeMap; 

public class SmallestFrequentNumberFinder { 

    public static int[] stringToIntegerArray(String[] stringArray) { 
     int[] integerArray = new int[stringArray.length]; 
     for (int i = 0; i < stringArray.length; i++) { 
      integerArray[i] = Integer.parseInt(stringArray[i]); 
     } 
     return integerArray; 
    } 

    public static int getSmallestFrequentNumber(int[] numbers) { 
     int max = -1; 
     Integer smallestFrequentNumber = null; 
     TreeMap<Integer, Integer> frequencyMaper = new TreeMap<Integer, Integer>(); 

     for (int number : numbers) { 
      Integer frequency = frequencyMaper.get(number); 
      frequencyMaper.put(number, (frequency == null) ? 1 : frequency + 1); 
     } 

     for (int number : frequencyMaper.keySet()) { 
      Integer frequency = frequencyMaper.get(number); 
      if (frequency != null && frequency > max) { 
       max = frequency; 
       smallestFrequentNumber = number; 
      } 
     } 
     return smallestFrequentNumber; 
    } 

    public static void main(String args[]) { 
     String[] numbersAsString = {"5", "5", "4", "2", "4", "4", "2", "2"}; 
     final int[] integerArray = stringToIntegerArray(numbersAsString); 
     System.out.println(getSmallestFrequentNumber(integerArray)); 
    } 
} 
相關問題