2017-02-12 79 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