Find frequency of smallest value in an array - GeeksforGeeks

11 May, 2021
Given an array A of N elements. Find the frequency of the smallest value in the array.Examples:  
Input : N = 5, arr[] = {3, 2, 3, 4, 4} 
Output : 1
The smallest element in the array is 2 
and it occurs only once.

Input : N = 6, arr[] = {4, 3, 5, 3, 3, 6}
Output : 3
The smallest element in the array is 3 
and it occurs 3 times.
 
Simple Approach: A simple method is to first find the minimum element in the array in first traversal. Then traverse the array again and find the number of occurrences of the minimum element.Efficient Approach: The efficient approach is to do this in a single traversal. Let us assume the first element to be the current minimum, so the frequency of the current minimum would be 1. Now let’s iterate through the array (1 to N), we come across 2 cases:  
Current element is smaller than our current minimum: We change the current minimum to be equal to current element and since this is the first time we are coming across this element, we make it’s frequency 1.
Current element is equal to the current minimum: We increment the frequency of current minimum.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find the frequency of// minimum element in the array #include <bits/stdc++.h>using namespace std; // Function to find the frequency of the// smallest value in the arrayint frequencyOfSmallest(int n, int arr[]){    int mn = arr[0], freq = 1;     for (int i = 1; i < n; i++) {         // If current element is smaller        // than minimum        if (arr[i] < mn) {            mn = arr[i];            freq = 1;        }        // If current element is equal        // to smallest        else if (arr[i] == mn)            freq++;    }     return freq;} // Driver Codeint main(){    int N = 5;    int arr[] = { 3, 2, 3, 4, 4 };     cout << frequencyOfSmallest(N, arr);     return 0;}
// Java program to find the frequency of// minimum element in the arrayimport java.io.*; class GFG{     // Function to find the frequency of the// smallest value in the arraystatic int frequencyOfSmallest(int n, int arr[]){    int mn = arr[0], freq = 1;     for (int i = 1; i < n; i++)    {         // If current element is smaller        // than minimum        if (arr[i] < mn)        {            mn = arr[i];            freq = 1;        }                 // If current element is equal        // to smallest        else if (arr[i] == mn)            freq++;    }    return freq;}     // Driver Code    public static void main (String[] args)    {        int N = 5;        int arr[] = { 3, 2, 3, 4, 4 };        System.out.println (frequencyOfSmallest(N, arr));    }} // This code is contributed by Tushil.
# Python 3 program to find the frequency of# minimum element in the array  # Function to find the frequency of the# smallest value in the arraydef frequencyOfSmallest(n,arr):    mn = arr[0]    freq = 1     for i in range(1,n):        # If current element is smaller        # than minimum        if (arr[i] < mn):            mn = arr[i]            freq = 1               # If current element is equal        # to smallest        elif(arr[i] == mn):            freq += 1     return freq # Driver Codeif __name__ == '__main__':    N = 5    arr = [3, 2, 3, 4, 4]     print(frequencyOfSmallest(N, arr)) # This code is contributed by# Surendra_Gangwar
// C# program to find the frequency of// minimum element in the arrayusing System; class GFG{             // Function to find the frequency of the    // smallest value in the array    static int frequencyOfSmallest(int n, int []arr)    {        int mn = arr[0], freq = 1;             for (int i = 1; i < n; i++)        {                 // If current element is smaller            // than minimum            if (arr[i] < mn)            {                mn = arr[i];                freq = 1;            }                         // If current element is equal            // to smallest            else if (arr[i] == mn)                freq++;        }        return freq;    }         // Driver Code    public static void Main()    {        int N = 5;        int []arr = { 3, 2, 3, 4, 4 };                     Console.WriteLine(frequencyOfSmallest(N, arr));    }} // This code is contributed by Ryuga
<?php// PHP program to find the frequency of// minimum element in the array // Function to find the frequency of the// smallest value in the arrayfunction frequencyOfSmallest($n, $arr){    $mn = $arr[0];    $freq = 1;     for ($i = 1; $i < $n; $i++)    {         // If current element is smaller        // than minimum        if ($arr[$i] < $mn)        {            $mn = $arr[$i];            $freq = 1;        }        // If current element is equal        // to smallest        else if ($arr[$i] == $mn)            $freq++;    }     return $freq;} // Driver Code$N = 5;$arr = array( 3, 2, 3, 4, 4 ); print(frequencyOfSmallest($N, $arr)); // This code is contributed by mits?>
<script>    // Javascript program to find the frequency of    // minimum element in the array    // required answer         // Function to find the frequency of the    // smallest value in the array    function frequencyOfSmallest(n, arr)    {        let mn = arr[0], freq = 1;               for (let i = 1; i < n; i++)        {                   // If current element is smaller            // than minimum            if (arr[i] < mn)            {                mn = arr[i];                freq = 1;            }                           // If current element is equal            // to smallest            else if (arr[i] == mn)                freq++;        }        return freq;    }         let N = 5;    let arr = [ 3, 2, 3, 4, 4 ];     document.write(frequencyOfSmallest(N, arr)); </script>
1
 
Time Complexity: O(N) 
jit_t
SURENDRA_GANGWAR
ankthon
Mithun Kumar
divyeshrabadiya07
school-programming
Algorithms
Arrays
Arrays
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
DSA Sheet by Love Babbar
Quadratic Probing in Hashing
Difference between Informed and Uninformed Search in AI
K means Clustering - Introduction
SCAN (Elevator) Disk Scheduling Algorithms
Arrays in Java
Arrays in C/C++
Stack Data Structure (Introduction and Program)
Program for array rotation
Largest Sum Contiguous Subarray