Given array of integers arr, check and return true if the integers can be grouped into pairs of 2 integers, where both integers in a pair are the same.
| Input Parameters |
Expected outputs |
| intArray = {4,3,3,1,4,1};
|
Pairs can be made as required.
|
| intArray = {4,4,4,4};
|
Pairs can be made as required
|
| intArray = {9,9,9,1,2,2,2};
|
Pairs *cannot* be made as required
|
| The java method should accept following input parameters: arr(integer array).
|
| If (arr.length%2 !=0), return false as pairs cannot be made using all integers if number of integers is odd.
|
| Initialize a variable hMap of type HashMap to hold integers from input arrays as keys, and the frequency of the integers as the values.
|
| Iterate through arr using a for loop with idx as a loop variable with initial value 0 and increment idx till it reaches arr.length-1:
If hMap does not contain key arr[idx], then put key-value pair arr[idx] and 1 to hMap.
If hMap contains key arr(idx) with value freq, then put key-value pair arr(idx) and freq+1 to hMap.
|
| After above loop completes, iterate through keys of hMap and check that the values (the integers frequencies) corresponding to keys are divisible by 2. If all integer frequencies are divisible by 2, then pairs can be formed successfully from the input array, so return true, else return false.
|
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
import java.util.HashMap;
import java.util.Iterator;
public class HashMapMakePairs {
public boolean hashMapMakePairs(int[] arr) throws Exception{
if (arr.length%2 !=0) {return false;}
HashMap< Integer,Integer> hMap=new HashMap< Integer, Integer>();
for (int idx=0; idx < arr.length; idx++) {
if (hMap.containsKey(arr[idx])){
int freq=hMap.get(arr[idx]);
hMap.put(arr[idx], ++freq);
}else {
hMap.put(arr[idx], 1);
}
}
Iterator itr=hMap.keySet().iterator();
int cntInt=0;
while (itr.hasNext()) {
cntInt=hMap.get(itr.next());
if (cntInt % 2!=0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
HashMapMakePairs hp=new HashMapMakePairs();
boolean retVal;
try {
int[] intArray1 = {4,3,3,1,4,1};
printArraySummary(intArray1, "Original Array:");
retVal=hp.hashMapMakePairs(intArray1);
if (retVal==true) {
System.out.print("Pairs can be made as required");
} else {
System.out.print("Pairs *cannot* be made as required");
}
int[] intArray2 = {4,4,4,4};
printArraySummary(intArray2, "Original Array:");
retVal=hp.hashMapMakePairs(intArray2);
if (retVal==true) {
System.out.print("Pairs can be made as required");
} else {
System.out.print("Pairs *cannot* be made as required");
}
int[] intArray3 = {9,9,9,1,2,2,2};
printArraySummary(intArray3, "Original Array:");
retVal=hp.hashMapMakePairs(intArray3);
if (retVal==true) {
System.out.print("Pairs can be made as required");
} else {
System.out.print("Pairs *cannot* be made as required");
}
int[] intArray4 = {1,2,1,2};
printArraySummary(intArray4, "Original Array:");
retVal=hp.hashMapMakePairs(intArray4);
if (retVal==true) {
System.out.print("Pairs can be made as required");
} else {
System.out.print("Pairs *cannot* be made as required");
}
int[] intArray5 = {2,3,3,2};
printArraySummary(intArray5, "Original Array:");
retVal=hp.hashMapMakePairs(intArray5);
if (retVal==true) {
System.out.print("Pairs can be made as required");
} else {
System.out.print("Pairs *cannot* be made as required");
}
}catch (Exception exception) {
System.out.print("Exception: "+ exception);
exception.printStackTrace();
}
}
public static void printArraySummary(int[] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("************************************************************************");
System.out.print(label+" : ");
int arrayIndex=0;
for (arrayIndex=0; arrayIndex< intArray.length; arrayIndex++) {
System.out.print(intArray[arrayIndex]);
if (arrayIndex< intArray.length-1) {System.out.print(",");}
}
System.out.println();
System.out.println("*************************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}