2017-02-12 5 views
3

私はJavaを使い慣れていないので、私の検索では何か探しているものがたくさんある。 私は要素のJava技術を使用して、数字に一致するUNIQUE回数を数えようとしています。 たとえば、配列{2,3,2,3,2}には2つの一意の一致するペアケース(2,2)と(3,3) があります(以下のコードを参照)。 TOTAL Machedペアがいくつあるかのカウントです。例の場合、結果は4つの場合(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)'は各番号の対の数を与えるべきですか? – Nic

+0

さらに詳しい状況では、この特定の状況では、1〜9の5つの値の配列に限定されています。また、私はまだ高度なトピックに進んでいないので、ループなどの基本的なテクニックで解決しようとしている – 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)); 

:ノー・修正元のデータ、単純なループ、唯一の「シンプル」なデータ構造を;

一つの方法は、あなたが(私はboolean型の配列でこれを行う)前の要素を見ているかどうかを追跡するために、次のようになります。

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を使いこなしている場合はもう少し複雑ですが、キーとして要素を含み、配列に値として表示される回数を含むマップを作成できます。そして、あなたは少なくとも2回発生する要素を探して、マップの値を横切ることができる(すなわち、少なくとも1組に属している):

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、3、3} - これは問題の仕様に従って3ペアですか? – Nic

+0

私が質問を読んでいるところでは、{2,2,2,2,3,3}は(2,2)と(3,3)の2つのユニークなペアになります。 – azurefrog

+0

ああ、もう一度見て、私はあなたが正しいと思います。 – Nic

関連する問題