Find frequency of smallest value in an array (original) (raw)
Last Updated : 15 Nov, 2022
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 its 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++ `
// 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 array 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 int main() { int N = 5; int arr[] = { 3, 2, 3, 4, 4 };
cout << frequencyOfSmallest(N, arr);
return 0;
}
Java
// Java program to find the frequency of // minimum element in the array import java.io.*;
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 (String[] args)
{
int N = 5;
int arr[] = { 3, 2, 3, 4, 4 };
System.out.println (frequencyOfSmallest(N, arr));
}
}
// This code is contributed by Tushil.
Python3
Python 3 program to find the frequency of
minimum element in the array
Function to find the frequency of the
smallest value in the array
def 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 Code
if name == 'main': N = 5 arr = [3, 2, 3, 4, 4]
print(frequencyOfSmallest(N, arr))
This code is contributed by
Surendra_Gangwar
C#
// C# program to find the frequency of // minimum element in the array using 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
JavaScript
`
Time Complexity: O(N), where N is the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Similar Reads
- Least frequent element in an array Given an array, find the least frequent element in it. If there are multiple elements that appear least number of times, print any one of them. Examples : Input : arr[] = {1, 3, 2, 1, 2, 2, 3, 1}Output : 3Explanation: 3 appears minimum number of times in given array. Input : arr[] = {10, 20, 30}Outp 11 min read
- Find m-th smallest value in k sorted arrays Given k sorted arrays of possibly different sizes, find m-th smallest value in the merged array.Examples: Input: m = 5 arr[][] = { {1, 3}, {2, 4, 6}, {0, 9, 10, 11}} ; Output: 4 Explanation The merged array would be {0 1 2 3 4 6 9 10 11}. The 5-th smallest element in this merged array is 4. Input: m 8 min read
- Find k smallest elements in an array Given an array arr[] and an integer k, the task is to find k smallest elements in the given array. Elements in the output array can be in any order.Examples:Input: arr[] = [1, 23, 12, 9, 30, 2, 50], k = 3Output: [1, 2, 9]Input: arr[] = [11, 5, 12, 9, 44, 17, 2], k = 2Output: [2, 5]Table of Content[A 15 min read
- Smallest perfect cube in an array Given an array arr[] of n integers. The task is to find the smallest perfect cube from the array. Print -1 if there is no perfect cube in the array.Examples: Input: arr[] = {16, 8, 25, 2, 3, 10} Output: 8 8 is the only perfect cube in the array Input: arr[] = {27, 8, 1, 64} Output: 1 All elements ar 6 min read
- Smallest greater elements in whole array An array is given of n length, and we need to calculate the next greater element for each element in the given array. If the next greater element is not available in the given array then we need to fill '_' at that index place. Examples : Input : 6 3 9 8 10 2 1 15 7 Output : 7 6 10 9 15 3 2 _ 8 Here 11 min read
- Kth smallest element from an array of intervals Given an array of intervals arr[] of size N, the task is to find the Kth smallest element among all the elements within the intervals of the given array. Examples: Input : arr[] = {{5, 11}, {10, 15}, {12, 20}}, K =12Output: 13Explanation: Elements in the given array of intervals are: {5, 6, 7, 8, 9, 7 min read
- Find the smallest and second smallest elements in an array Given an array arr[] of size N, find the smallest and second smallest element in an array.Examples:Input: arr[] = {12, 13, 1, 10, 34, 1}Output: 1 10Explanation: The smallest element is 1 and second smallest element is 10.Input: arr[] = {111, 13, 25, 9, 34, 1}Output: 1 9Explanation: The smallest elem 12 min read
- Find closest smaller value for every element in array Given an array of integers, find the closest smaller element for every element. If there is no smaller element then print -1 Examples: Input : arr[] = {10, 5, 11, 6, 20, 12} Output : 6, -1, 10, 5, 12, 11 Input : arr[] = {10, 5, 11, 10, 20, 12} Output : 5 -1 10 5 12 11 A simple solution is to run two 4 min read
- K’th Smallest Element in Unsorted Array Given an array arr[] of N distinct elements and a number K, where K is smaller than the size of the array. Find the K'th smallest element in the given array. Examples:Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 3 Output: 7Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 4 Output: 10 Table of Content[Naive Ap 15 min read
- Python heapq to find K'th smallest element in a 2D array Given an n x n matrix and integer k. Find the k'th smallest element in the given 2D array. Examples: Input : mat = [[10, 25, 20, 40], [15, 45, 35, 30], [24, 29, 37, 48], [32, 33, 39, 50]] k = 7 Output : 7th smallest element is 30 We will use similar approach like K’th Smallest/Largest Element in Uns 3 min read