How to find Frequency of the Numbers from the given array of Integers ?

This is the complete solution to find the frequency numbers from given array of the numbers This solution is designed to to solve the problem using an interface, super class and three derived classes implementing the interface.

package pack1;

public interface FrequencyNumIntf {

	/* Define the functional interface with SAM 'RdInputArray' */
	public int [] RdInputArray(Integer[] arrayNums);

	/* Define the static method 'displayMsg' */
	static void displayMsg(int [] freqArray) {

		System.out.println("Total number of Pairs, available in the given array are = "+freqArray[0]);
		System.out.println("Total number of Triplets, available in the given array are = "+freqArray[1]);
	}
}
package pack1;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.logging.Logger;
import java.util.stream.Collectors;

class ReadInpArrayNums {

	private Integer[] inputArrayNums;

	/* Declare the logger object for the class to display the log messages */
	static Logger logger = Logger.getLogger(ReadInpArrayNums.class.getName());

	public boolean validateInputs(Integer[] inputArrayNums) {

		/* Fetch the class object's name from which this method is called */
		String cName = Thread.currentThread().getStackTrace()[2].getClassName();
		String mName = "validateInputs()";
		logger.info("This method '" + mName + "' called from the Class '" + cName + "'");

		/* Validating the function parameters using Optional Integer Array data type */
		Optional<Integer[]> inputArrayVals= Optional.ofNullable(inputArrayNums);

		/*
		 * Fetch the value of the Optional Integer Array of inputArrayNums using 'isPresent'
		 * condition check. If the value is available use 'get' method otherwise simply
		 * return 'false' to stop the program execution without proceeding further.
		 */
		if (inputArrayVals.isPresent()) {
			if (inputArrayNums.length != 0)
				setInputArrayNums(inputArrayVals.get());
			else
				return false;
		} else
			return false;

		logger.info("The provided Input validated Successfully");
		return true;
	}

	/* Define getter and setters for an inputArrayNums array values */
	public Integer[] getInputArrayNums() {
		return inputArrayNums;
	}

	public void setInputArrayNums(Integer[] inputArrayNums) {
		this.inputArrayNums = inputArrayNums;
	}
}

class FrequencyNums_Using_Hashmap extends ReadInpArrayNums implements FrequencyNumIntf {

	public int [] RdInputArray(Integer[] inpVal) {

		/* Fetch the class object's name from which this method is called */
		String cName = Thread.currentThread().getStackTrace()[2].getClassName();
		String mName = "RdInputArray";
		logger.info("This method '" + mName + "' called from the Class '" + cName + "'");

		//To count the number of pairs and triplets in an array of integers.
		int [] freqNumArray = new int [2];
		int numberOfPairs=0,numberOfTriplets=0;
		HashMap<Integer, Integer> availablePairs=new HashMap<Integer, Integer> ();

		if (validateInputs(inpVal)) {

			// Create a HashMap of the given array of Integers
			for(int i=0;i<getInputArrayNums().length;i++) {
				if (!availablePairs.containsKey(getInputArrayNums()[i]))
					availablePairs.put(getInputArrayNums()[i], 1);
				else
					availablePairs.put(getInputArrayNums()[i], availablePairs.get(getInputArrayNums()[i])+1);
			}
			//Traversing through the HashMap for checking the any of the HashMap entries value for 2 & 3
			for(Entry<Integer, Integer> mapOfNums:availablePairs.entrySet()) {
				if(mapOfNums.getValue()==2)
					numberOfPairs++;
				if(mapOfNums.getValue()==3)
					numberOfTriplets++;
			}
			freqNumArray[0]=numberOfPairs;
			freqNumArray[1]=numberOfTriplets;
		} else
			logger.info("The given Input Array numbers are null");

		FrequencyNumIntf.displayMsg(freqNumArray);
		return freqNumArray;
	}
}

class FrequencyNums_Using_Collections extends ReadInpArrayNums implements FrequencyNumIntf {

	public int [] RdInputArray(Integer[] inpVal) {

		/* Fetch the class object's name from which this method is called */
		String cName = Thread.currentThread().getStackTrace()[2].getClassName();
		String mName = "RdInputArray";
		logger.info("This method '" + mName + "' called from the Class '" + cName + "'");

		//To count the number of pairs and triplets in an array of integers.
		int [] freqNumArray = new int [2];
		int numberOfPairs=0,numberOfTriplets=0;

		if (validateInputs(inpVal)) {

			//Converting the array of integers to ArrayList using Arrays class
			List<Integer> arrIntList=Arrays.stream(getInputArrayNums()).collect(Collectors.toList());

			HashSet<Integer> uniqueInts=new HashSet<Integer>(arrIntList);

			//Use the Collections class to check frequency of any of ArrayList integers is to 2 or 3
			for (int numInt: uniqueInts) {
				if (Collections.frequency(arrIntList,numInt)==2) {
					numberOfPairs++;
				}
				if (Collections.frequency(arrIntList,numInt)==3) {
					numberOfTriplets++;
				}
			}
			freqNumArray[0]=numberOfPairs;
			freqNumArray[1]=numberOfTriplets;
		} else
			logger.info("The given Input Array numbers are null");

		FrequencyNumIntf.displayMsg(freqNumArray);
		return freqNumArray;
	}
}


class FrequencyNums_Using_LambdaExpression extends ReadInpArrayNums implements FrequencyNumIntf {

	public int [] RdInputArray(Integer[] inpVal) {

		/* Fetch the class object's name from which this method is called */
		String cName = Thread.currentThread().getStackTrace()[2].getClassName();
		String mName = "RdInputArray";
		logger.info("This method '" + mName + "' called from the Class '" + cName + "'");

		//To count the number of pairs and triplets in an array of integers.
		int [] freqNumArray = new int [2];
		HashMap<Integer, Integer> availablePairs = new HashMap<Integer, Integer>();
		final int[] numberOfPairs = { 0 };
		final int[] numberOfTriplets = { 0 };

		if (validateInputs(inpVal)) {

			// Create a HashMap of the given array of Integers
			for (int i = 0; i < getInputArrayNums().length; i++) {
				if (!availablePairs.containsKey(getInputArrayNums()[i]))
					availablePairs.put(getInputArrayNums()[i], 1);
				else
					availablePairs.put(getInputArrayNums()[i], availablePairs.get(getInputArrayNums()[i]) + 1);
			}
			/* Using the Lambda expression, traversing through the HashMap for checking the
			 * any of the HashMap entrie's value is 2 or 3 */
			availablePairs.forEach((k, v) -> {
				if (v == 2)
					numberOfPairs[0] += 1;
				if (v == 3)
					numberOfTriplets[0] += 1;
			});
			freqNumArray[0]=numberOfPairs[0];
			freqNumArray[1]=numberOfTriplets[0];

		} else
			logger.info("The given Input Array numbers are null");

		FrequencyNumIntf.displayMsg(freqNumArray);
		return freqNumArray;
	}
}


public class FrequencyNumJ8 {

	public static void main(String[] args) {

		/*
		 * create the class object FrequencyNums_Using_Hashmap, and call the method
		 * RdInputArray
		 */
		Integer [] numArray= {1,3,4,1,6,9,4,3,7,8,3};
		FrequencyNums_Using_Hashmap FNUH = new FrequencyNums_Using_Hashmap();
		FNUH.RdInputArray(numArray);

		/*
		 * create the class object FrequencyNums_Using_Collections, and call the method
		 * RdInputArray
		 */

		Integer [] numArray1= {91,43,54,91,36,91,14,43,17,28,36,54,43};
		FrequencyNums_Using_Collections FNUC = new FrequencyNums_Using_Collections();
		FNUC.RdInputArray(numArray1);

		/*
		 * create the class object FrequencyNums_Using_LambdaExpression, and call the method
		 * RdInputArray
		 */

		Integer [] numArray2 = { 9, 6, 1, 3, 8, 4, 1, 6, 9, 4, 3, 7, 8, 3, 6, 9};
		FrequencyNums_Using_LambdaExpression FNULE = new FrequencyNums_Using_LambdaExpression();
		FNULE.RdInputArray(numArray2);
	}
}
OUTPUT:

Jun 26, 2023 9:59:21 PM pack1.FrequencyNums_Using_Hashmap RdInputArray
INFO: This method 'RdInputArray' called from the Class 'pack1.FrequencyNumJ8'
Jun 26, 2023 9:59:21 PM pack1.ReadInpArrayNums validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.FrequencyNums_Using_Hashmap'
Jun 26, 2023 9:59:21 PM pack1.ReadInpArrayNums validateInputs
INFO: The provided Input validated Successfully
Total number of Pairs, available in the given array are = 2
Total number of Triplets, available in the given array are = 1
Jun 26, 2023 9:59:21 PM pack1.FrequencyNums_Using_Collections RdInputArray
INFO: This method 'RdInputArray' called from the Class 'pack1.FrequencyNumJ8'
Jun 26, 2023 9:59:21 PM pack1.ReadInpArrayNums validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.FrequencyNums_Using_Collections'
Jun 26, 2023 9:59:21 PM pack1.ReadInpArrayNums validateInputs
INFO: The provided Input validated Successfully
Total number of Pairs, available in the given array are = 2
Total number of Triplets, available in the given array are = 2
Jun 26, 2023 9:59:21 PM pack1.FrequencyNums_Using_LambdaExpression RdInputArray
INFO: This method 'RdInputArray' called from the Class 'pack1.FrequencyNumJ8'
Jun 26, 2023 9:59:21 PM pack1.ReadInpArrayNums validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.FrequencyNums_Using_LambdaExpression'
Jun 26, 2023 9:59:21 PM pack1.ReadInpArrayNums validateInputs
INFO: The provided Input validated Successfully
Total number of Pairs, available in the given array are = 3
Total number of Triplets, available in the given array are = 3

The link to the JUnit5 test for this solution is here – https://atomic-temporary-185308886.wpcomstaging.com/2023/06/28/junit-test-frequency-nums/