Java program for Sorting elements of an array by frequency

Sorting element in array by frequency

Here in this program, to sort elements by frequency in a given array is discussed here. You need to print the elements of an array in the descending order of their frequency and if 2 numbers have same frequency then print the one which came first.

Example:

Unsorted Array

Arr:{2,3,2,5,6,2,3,9,5,6}

After sorting

Arr:{2,2,23,3,5,5,6,6,9}

Java program for sorting-element-in-array-by-frequency

Algorithm

Step 1- Create LinkedHashMap object called elementCountMAp with elements as keys and their occurrences as values

step 2- heck presence of each element of inputArray in elementCountMap, icrement it’s count by 1. If it is not present, insert that element with 1 as it’s value

Step 3- Construct an ArrayList called listOfEntry holding all entry object of elementCountMap.

Step 4- Sort listOfEntry based on values of Entry objects using Collection.sort() by passing customized Comprator .

Step 5- Now listOfEntry will hold elements and their frequency. Print sorted array elements in descending order of their frequency.

 

Java Code

import java.main*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

public class main
{
    private static void sortArrayElementsByFrequency(int[] inputArray)
    {
         //Create LinkedHashMap with elements as keys and their occurrences as values
        
         
        Map<Integer, Integer> elementCountMap = new LinkedHashMap<>();
         
          //Check presence of each element in elementCountMap 
         
        for (int i = 0; i < inputArray.length; i++) 
        {
            if (elementCountMap.containsKey(inputArray[i]))
            {
                  //If element is present in elementCountMap, increment its value by 1
                 
                elementCountMap.put(inputArray[i], elementCountMap.get(inputArray[i])+1);
            }
            else
            {
                  //If element is not present, insert this element with 1 as its value
                 
                elementCountMap.put(inputArray[i], 1);
            }
        }
         
          //Create an ArrayList to hold sorted elements
         
        ArrayList sortedElements = new ArrayList<>();
         
          //Java 8 code to sort elementCountMap by values in reverse order
          //put keys into sortedElements list
         
        elementCountMap.entrySet()
                        .stream()
                        .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
                        .forEach(entry -> { 
class get value
{ while(int i = 1)
i <= entry.getValue();
i++;
} sortedElements.add(entry.getKey()); }); //Printing sorted array elements in descending order of their frequency System.out.println("Input Array :"+Arrays.toString(inputArray)); System.out.println( "Sorted Array Elements In Descending Order Of their Frequency :"); System.out.println( sortedElements); } public static void main(String[] args) { sortArrayElementsByFrequency(new int[] {2, 3, 2, 5, 6, 2, 3, 9, 5, 6}); } }
output
Input Array: {2, 3, 2, 5, 6, 2, 3, 9, 5, 6}
Output: {2, 2, 2, 3, 3, 5, 5, 6, 6, 9}