Python Program for sorting elements of an Array by Frequency

Program for sorting elements of an Array by Frequency

 

Here in this Article we will learn about Python Program for sorting elements of an Array by Frequency .

We need to print the elements of an array in the descending order of their frequency and if two elements have same frequency then print the one which came first.

Python Program for sorting elements of an Array by Frequency

Keypoint

In this section we will learn about basic knowledge which we need to know before coding the above Program. So we must have knowledge of what is an array? 

What is an array?
An array is a data structure, it is collection of similar data elements which is stored at contiguous memory locations in which each data element can be accessed directly by only using its index number.
 
About Python language:-
Python is a popular programming language. Python is a high level language that will make a programmer focus on what to do instead of how to do.
Its design philosophy emphasizes code readability with its use of significant indentation. Its language constructs as well as its object-oriented approach aim to help programmers write clear, logical code for small and large-scale 
 
 
How to declare an array?
To declare an array in C,a programmer specifies the type of the elements and the number of elements required by an array as follows − This is called a single-dimensional array. The arraySize must be an integer constant greater than zero and type can be any valid C data type. For example, to declare a 10-element array called balanceof type double, use this statement − Here balanceis a variable array which is sufficient to hold up to 10 double numbers

Algorithm :

  • Take an array and length of the array as input
  • Call user defined function sortByFreq(arr,n)
  • Print the resulting array
sortByFreq(arr,n)
  • Create a hashmap using defaultdict function present in collections module of python
  • Check presence of each element of inputArray in d, increment it’s count by 1.
  • Sorting the array ‘arr’ where key is the function based on which the array is sorted. (While sorting we want to give first priority to frequency then to value of item)
  • Return sorted array
 
Sorting elements of an array by frequency in Python

 Code For Python Program for sorting elements of an Array by Frequency 

# Python program to Sort by Frequency

from collections import defaultdict

def sortByFreq(arr, n):

# d is a hashmap(referred as dictionary in python)

d = defaultdict(lambda: 0)

for i in range(n):

   d[arr[i]] += 1

# Sorting the array 'arr' where key

# is the function based on which

# the array is sorted

# While sorting we want to give

# first priority to Frequency

# Then to value of item

arr.sort(key=lambda x: (-d[x], x))


return (arr)

# Driver Function

if __name__ == "__main__":

    arr = [2, 5, 2, 6, -1, 
      9999999, 5, 8, 8, 8]

     n = len(arr)

    solution = sortByFreq(arr, 
    n)

    print(*solution)
Output :

8 8 8 2 2 5 5 -1 6 9999999