hasTwoPair
Category: Programming
Author: Stuart Reges
Book Chapter: 7.2
Problem: hasTwoPair
Write a static method called hasTwoPair that takes an array of integers in the range of 1 to 6 as a parameter and that returns whether or not the array contains two values that appear twice (true if it does, false if it does not). This is a problem from the game of Yahtzee in which players roll five dice and look for various combinations, but your solution should not depend on the array containing five values. You can, however, make use of the fact that all of the numbers will be in the range of 1 to 6. Your method should return true when exactly two values appearing in the array occur exactly two times. If one value appears two times and another appears three times, that would not count as two pairs. Similarly, if there are three numbers that appear two times, that would not count as two pairs. There must be exactly two such numbers and each must occur exactly two times. Below are examples of arrays and the value that should be returned for each by hasTwoPair: Contents of array Value returned ------------------ -------------- {2, 4, 2, 2, 4} false {3, 4, 3, 6, 6} true {4, 1, 4, 4, 2} false {5, 5, 3, 3, 4} true {6, 2, 6, 5, 3} false {1, 3, 5, 3, 1} true {3, 1, 3, 1} true {1, 2, 3, 1, 2, 3} false