2017-02-12 31 views
3

我是java的新手,並且通過我的搜索在ALMOST上發現了許多帖子,但我不太清楚。 我正在嘗試使用基本的java技術來統計一個數字匹配的UNIQUE次數。 例如,數組{2,3,2,3,2}將有兩個唯一的匹配對案例(2,2)和(3,3) 到目前爲止(見下面的代碼),我似乎可以想出所有是有多少TOTAL配對的計數。在示例情況下,結果將是四種情況(2,2),(2,2),(3,3),(2,2)。要清楚這是第一學期問題類型的東西,所以我不能使用地圖或更先進的技術。簡單循環與計數和迭代。由於在Java數組中計數非重複匹配對

int count = 0; 
    for(int i=0;i<=hand.length-2 ;i++) 
    { 
     for(int j=i+1;j<=hand.length-1;j++) 
     { 
      if (hand[j] == hand[i]) 
      { 

       count = count + 1; 
      } 
     } 
    } 
    System.out.println(count); 
+0

數字是否有合理的界限?就像,他們都是1到100之間的整數或者什麼? –

+0

我不認爲你實際上需要構建所有的對來解決你的問題,如果它只是一個數。如果將數組轉換爲映射條目到出現次數的「Map 」,那麼floor(count/2)應該給出每個數字對的數目no? – Nic

+0

更多的上下文,在這個特定的情況下,我被限制在從1-9範圍內的五個值的數組。還試圖用循環等基本技術來解決,因爲我還沒有進入更高級的主題 – tmoesabi

回答

1

@azurefrog給出了很好的答案了。下面是計算有一個給定數量超過3項對實現:

List<Integer> numbers = Arrays.asList(2, 3, 2, 3, 2, 2, 9); 
Map<Integer, Long> map = numbers.stream() 
     .collect(Collectors.groupingBy(num -> num, Collectors.counting())) 
     .entrySet() 
     .stream() 
     .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue()/2)); 

// output 
map.forEach((num, count) -> System.out.println(String.format("%d has %d unique pairs", num, count))); 
Long total = map.values().stream().reduce((acc, c) -> c + acc).get(); 
System.out.print(String.format("A total of %d pairs", total)); 

考慮到在評論中列出的其他限制:沒有對矯正原始數據,簡單的循環,只是「簡單」的數據結構;

一種方法是跟蹤你是否見過一個元素(我做這個用布爾數組):

int[] hand = {2,3,2,3,2,9,5,5,5,5,5,5,5}; 
boolean[] hasPair = new boolean[10]; 
for(int i = 0; i <= hand.length - 2 ; i++) { 
    for(int j= i + 1; j <= hand.length - 1; j++) { 
     if (hand[j] == hand[i]) { 
      hasPair[hand[j]] = true; 
     } 
    } 
} 
int count = 0; 
for (boolean b : hasPair) { 
    if (b) { 
     count += 1; 
    } 
} 
System.out.print(count); 

這計數獨特的雙或「複製」,假定輸入數組爲int的在{1,...,9}

1

的Java 8

如果你可以使用Java 8,這是非常簡單易用的流API將多達元素,並檢查其中有多少屬於至少一對:

Integer[] data = { 2, 3, 2, 3, 2 }; 

    // create a map of each value to a list containing all instances of that value in the array 
    Map<Integer, List<Integer>> map = Arrays.stream(data).collect(Collectors.groupingBy(i -> i)); 

    // count how many of those lists have more than one element, i.e. pairs 
    long uniquePairs = map.values().stream().filter(l -> l.size() > 1).count(); 

    System.out.println(uniquePairs); 

的Java 7

如果您使用Java 7時遇到困難,這會稍微複雜一點,但您可以創建一個包含元素作爲鍵的映射,並將它們作爲值出現在數組中的次數。然後,你可以穿越地圖的價值尋找發生至少兩次元素(即至少屬於一對):

Integer[] data = { 2, 3, 2, 3, 2 }; 

    // create a map of each element to a count of the times that element appears in the array 
    Map<Integer, Integer> map = new HashMap<>(); 
    for (int i : data) { 
     Integer oldCount = map.get(i); 
     int newCount = oldCount == null ? 1 : oldCount + 1; 
     map.put(i, newCount); 
    } 

    // count the number of elements that appear more than once, i.e. pairs 
    int uniquePairs = 0; 
    for (int i : map.values()) { 
     if (i > 1) uniquePairs++; 
    } 

    System.out.println(uniquePairs); 
+0

我不認爲這會計算入口數大於3的配對,例如:{2,2,2,2,3,3} - 根據問題規格,這是否應該是3對? – Nic

+0

我讀到這個問題的方式{2,2,2,2,3,3}將是2個唯一對,(2,2)和(3,3)。 – azurefrog

+0

啊,是的,第二次看,我認爲你是對的。 – Nic