Find frequency of smallest element in an array
Here given code implementation process.
// C Program
// Find frequency of smallest element in an array
#include <stdio.h>
//Function which is display array elements
void display(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
printf(" %d", arr[i]);
}
printf("\n");
}
// Returns the location of smallest element of array
int findSmallest(int arr[], int size)
{
int location = 0;
for (int i = 1; i < size; ++i)
{
if (arr[location] > arr[i])
{
location = i;
}
}
return location;
}
// Count frequency of smallest element in given array
void frequencyOfSmallest(int arr[], int size)
{
if (size <= 1)
{
// When have less than 2 elements
return;
}
// Display array elements
printf("\n Given Array \n");
display(arr, size);
// Find the location of smallest element
int location = findSmallest(arr, size);
// Define counter variable
int counter = 0;
// Execute loop through by array size
for (int i = 0; i < size; ++i)
{
if (arr[location] == arr[i])
{
// When get smallest element
counter++;
}
}
// Display the counter frequency
printf(" Smallest element [%d] are exists of %d times \n", arr[location], counter);
}
int main(int argc, char
const *argv[])
{
// Define array of integer elements
int arr[] = {
2 , 5 , 1 , 2 , 6 , 4 , 1 , 6 , 7 , 3 , 1
};
// Get the size
int size = sizeof(arr) / sizeof(arr[0]);
frequencyOfSmallest(arr, size);
return 0;
}
Output
Given Array
2 5 1 2 6 4 1 6 7 3 1
Smallest element [1] are exists of 3 times
/*
Java Program for
Find frequency of smallest element in an array
*/
class Frequency
{
//Function which is display array elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print(" " + arr[i]);
}
System.out.print("\n");
}
// Returns the location of smallest element of array
public int findSmallest(int[] arr, int size)
{
int location = 0;
// Find smallest element
for (int i = 1; i < size; ++i)
{
if (arr[location] > arr[i])
{
location = i;
}
}
return location;
}
// Count frequency of smallest element in given array
public void frequencyOfSmallest(int[] arr, int size)
{
if (size <= 1)
{
// When have less than 2 elements
return;
}
// Display array elements
System.out.print("\n Given Array \n");
display(arr, size);
// Find the location of smallest element
int location = findSmallest(arr, size);
// Define counter variable
int counter = 0;
// Execute loop through by array size
for (int i = 0; i < size; ++i)
{
if (arr[location] == arr[i])
{
// When get smallest element
counter++;
}
}
// Display the number of frequency
System.out.print(" Smallest element " + arr[location] + " are exists of " + counter + " times \n");
}
public static void main(String[] args)
{
Frequency task = new Frequency();
// Define array of integer elements
int[] arr = {
2 , 5 , 1 , 2 , 6 , 4 , 1 , 6 , 7 , 3 , 1
};
// Get the size
int size = arr.length;
task.frequencyOfSmallest(arr, size);
}
}
Output
Given Array
2 5 1 2 6 4 1 6 7 3 1
Smallest element 1 are exists of 3 times
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program for
Find frequency of smallest element in an array
*/
class Frequency
{
public:
//Function which is display array elements
void display(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << " " << arr[i];
}
cout << "\n";
}
// Returns the location of smallest element of array
int findSmallest(int arr[], int size)
{
int location = 0;
// Find smallest element
for (int i = 1; i < size; ++i)
{
if (arr[location] > arr[i])
{
location = i;
}
}
return location;
}
// Count frequency of smallest element in given array
void frequencyOfSmallest(int arr[], int size)
{
// When have less than 2 elements
if (size <= 1)
{
return;
}
// Display array elements
cout << "\n Given Array \n";
this->display(arr, size);
// Find the location of smallest element
int location = this->findSmallest(arr, size);
// Define counter variable
int counter = 0;
// Execute loop through by array size
for (int i = 0; i < size; ++i)
{
if (arr[location] == arr[i])
{
// When get smallest element
counter++;
}
}
// Display the number of frequency
cout << " Smallest element " << arr[location] << " are exists of " << counter << " times \n";
}
};
int main()
{
Frequency task = Frequency();
// Define array of integer elements
int arr[] = {
2 , 5 , 1 , 2 , 6 , 4 , 1 , 6 , 7 , 3 , 1
};
// Get the size
int size = sizeof(arr) / sizeof(arr[0]);
task.frequencyOfSmallest(arr, size);
return 0;
}
Output
Given Array
2 5 1 2 6 4 1 6 7 3 1
Smallest element 1 are exists of 3 times
// Include namespace system
using System;
/*
C# Program for
Find frequency of smallest element in an array
*/
public class Frequency
{
//Function which is display array elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write(" " + arr[i]);
}
Console.Write("\n");
}
// Returns the location of smallest element of array
public int findSmallest(int[] arr, int size)
{
int location = 0;
// Find smallest element
for (int i = 1; i < size; ++i)
{
if (arr[location] > arr[i])
{
location = i;
}
}
return location;
}
// Count frequency of smallest element in given array
public void frequencyOfSmallest(int[] arr, int size)
{
// When have less than 2 elements
if (size <= 1)
{
return;
}
// Display array elements
Console.Write("\n Given Array \n");
display(arr, size);
// Find the location of smallest element
int location = findSmallest(arr, size);
// Define counter variable
int counter = 0;
// Execute loop through by array size
for (int i = 0; i < size; ++i)
{
if (arr[location] == arr[i])
{
// When get smallest element
counter++;
}
}
// Display the number of frequency
Console.Write(" Smallest element " + arr[location] + " are exists of " + counter + " times \n");
}
public static void Main(String[] args)
{
Frequency task = new Frequency();
// Define array of integer elements
int[] arr = {
2 , 5 , 1 , 2 , 6 , 4 , 1 , 6 , 7 , 3 , 1
};
// Get the size
int size = arr.Length;
task.frequencyOfSmallest(arr, size);
}
}
Output
Given Array
2 5 1 2 6 4 1 6 7 3 1
Smallest element 1 are exists of 3 times
<?php
/*
Php Program for
Find frequency of smallest element in an array
*/
class Frequency
{
//Function which is display array elements
public function display($arr, $size)
{
for ($i = 0; $i < $size; ++$i)
{
echo " ". $arr[$i];
}
echo "\n";
}
// Returns the location of smallest element of array
public function findSmallest( $arr, $size)
{
$location = 0;
// Find smallest element
for ($i = 1; $i < $size; ++$i)
{
if ($arr[$location] > $arr[$i])
{
$location = $i;
}
}
return $location;
}
// Count frequency of smallest element in given array
public function frequencyOfSmallest( $arr, $size)
{
// When have less than 2 elements
if ($size <= 1)
{
return;
}
// Display array elements
echo "\n Given Array \n";
$this->display($arr, $size);
// Find the location of smallest element
$location = $this->findSmallest($arr, $size);
// Define counter variable
$counter = 0;
// Execute loop through by array size
for ($i = 0; $i < $size; ++$i)
{
if ($arr[$location] == $arr[$i])
{
// When get smallest element
$counter++;
}
}
// Display the number of frequency
echo " Smallest element ". $arr[$location] ." are exists of ". $counter ." times \n";
}
}
function main()
{
$task = new Frequency();
// Define array of integer elements
$arr = array(2, 5, 1, 2, 6, 4, 1, 6, 7, 3, 1);
// Get the size
$size = count($arr);
$task->frequencyOfSmallest($arr, $size);
}
main();
Output
Given Array
2 5 1 2 6 4 1 6 7 3 1
Smallest element 1 are exists of 3 times
/*
Node Js Program for
Find frequency of smallest element in an array
*/
class Frequency
{
//Function which is display array elements
display(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write(" " + arr[i]);
}
process.stdout.write("\n");
}
// Returns the location of smallest element of array
findSmallest(arr, size)
{
var location = 0;
// Find smallest element
for (var i = 1; i < size; ++i)
{
if (arr[location] > arr[i])
{
location = i;
}
}
return location;
}
// Count frequency of smallest element in given array
frequencyOfSmallest(arr, size)
{
// When have less than 2 elements
if (size <= 1)
{
return;
}
// Display array elements
process.stdout.write("\n Given Array \n");
this.display(arr, size);
// Find the location of smallest element
var location = this.findSmallest(arr, size);
// Define counter variable
var counter = 0;
// Execute loop through by array size
for (var i = 0; i < size; ++i)
{
if (arr[location] == arr[i])
{
// When get smallest element
counter++;
}
}
// Display the number of frequency
process.stdout.write(" Smallest element " + arr[location] + " are exists of " + counter + " times \n");
}
}
function main()
{
var task = new Frequency();
// Define array of integer elements
var arr = [2, 5, 1, 2, 6, 4, 1, 6, 7, 3, 1];
// Get the size
var size = arr.length;
task.frequencyOfSmallest(arr, size);
}
main();
Output
Given Array
2 5 1 2 6 4 1 6 7 3 1
Smallest element 1 are exists of 3 times
# Python 3 Program for
# Find frequency of smallest element in an array
class Frequency :
# Function which is display array elements
def display(self, arr, size) :
i = 0
while (i < size) :
print(" ", arr[i], end = "")
i += 1
print(end = "\n")
# Returns the location of smallest element of array
def findSmallest(self, arr, size) :
location = 0
i = 1
# Find smallest element
while (i < size) :
if (arr[location] > arr[i]) :
location = i
i += 1
return location
# Count frequency of smallest element in given array
def frequencyOfSmallest(self, arr, size) :
# When have less than 2 elements
if (size <= 1) :
return
# Display array elements
print("\n Given Array ")
self.display(arr, size)
# Find the location of smallest element
location = self.findSmallest(arr, size)
# Define counter variable
counter = 0
i = 0
# Execute loop through by array size
while (i < size) :
if (arr[location] == arr[i]) :
# When get smallest element
counter += 1
i += 1
# Display the number of frequency
print(" Smallest element", arr[location] ,"are exists of ", counter ," times ")
def main() :
task = Frequency()
# Define array of integer elements
arr = [2, 5, 1, 2, 6, 4, 1, 6, 7, 3, 1]
# Get the size
size = len(arr)
task.frequencyOfSmallest(arr, size)
if __name__ == "__main__": main()
Output
Given Array
2 5 1 2 6 4 1 6 7 3 1
Smallest element 1 are exists of 3 times
# Ruby Program for
# Find frequency of smallest element in an array
class Frequency
# Function which is display array elements
def display(arr, size)
i = 0
while (i < size)
print(" ", arr[i])
i += 1
end
print("\n")
end
# Returns the location of smallest element of array
def findSmallest(arr, size)
location = 0
i = 1
# Find smallest element
while (i < size)
if (arr[location] > arr[i])
location = i
end
i += 1
end
return location
end
# Count frequency of smallest element in given array
def frequencyOfSmallest(arr, size)
# When have less than 2 elements
if (size <= 1)
return
end
# Display array elements
print("\n Given Array \n")
self.display(arr, size)
# Find the location of smallest element
location = self.findSmallest(arr, size)
# Define counter variable
counter = 0
i = 0
# Execute loop through by array size
while (i < size)
if (arr[location] == arr[i])
# When get smallest element
counter += 1
end
i += 1
end
# Display the number of frequency
print(" Smallest element ", arr[location] ," are exists of ", counter ," times \n")
end
end
def main()
task = Frequency.new()
# Define array of integer elements
arr = [2, 5, 1, 2, 6, 4, 1, 6, 7, 3, 1]
# Get the size
size = arr.length
task.frequencyOfSmallest(arr, size)
end
main()
Output
Given Array
2 5 1 2 6 4 1 6 7 3 1
Smallest element 1 are exists of 3 times
/*
Scala Program for
Find frequency of smallest element in an array
*/
class Frequency
{
//Function which is display array elements
def display(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print(" " + arr(i));
i += 1;
}
print("\n");
}
// Returns the location of smallest element of array
def findSmallest(arr: Array[Int], size: Int): Int = {
var location: Int = 0;
var i: Int = 1;
// Find smallest element
while (i < size)
{
if (arr(location) > arr(i))
{
location = i;
}
i += 1;
}
return location;
}
// Count frequency of smallest element in given array
def frequencyOfSmallest(arr: Array[Int], size: Int): Unit = {
// When have less than 2 elements
if (size <= 1)
{
return;
}
// Display array elements
print("\n Given Array \n");
this.display(arr, size);
// Find the location of smallest element
var location: Int = this.findSmallest(arr, size);
// Define counter variable
var counter: Int = 0;
var i: Int = 0;
// Execute loop through by array size
while (i < size)
{
if (arr(location) == arr(i))
{
// When get smallest element
counter += 1;
}
i += 1;
}
// Display the number of frequency
print(" Smallest element " + arr(location) + " are exists of " + counter + " times \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Frequency = new Frequency();
// Define array of integer elements
var arr: Array[Int] = Array(2, 5, 1, 2, 6, 4, 1, 6, 7, 3, 1);
// Get the size
var size: Int = arr.length;
task.frequencyOfSmallest(arr, size);
}
}
Output
Given Array
2 5 1 2 6 4 1 6 7 3 1
Smallest element 1 are exists of 3 times
/*
Swift 4 Program for
Find frequency of smallest element in an array
*/
class Frequency
{
//Function which is display array elements
func display(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print(" ", arr[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
// Returns the location of smallest element of array
func findSmallest(_ arr: [Int], _ size: Int)->Int
{
var location: Int = 0;
var i: Int = 1;
// Find smallest element
while (i < size)
{
if (arr[location] > arr[i])
{
location = i;
}
i += 1;
}
return location;
}
// Count frequency of smallest element in given array
func frequencyOfSmallest(_ arr: [Int], _ size: Int)
{
// When have less than 2 elements
if (size <= 1)
{
return;
}
// Display array elements
print("\n Given Array ");
self.display(arr, size);
// Find the location of smallest element
let location: Int = self.findSmallest(arr, size);
// Define counter variable
var counter: Int = 0;
var i: Int = 0;
// Execute loop through by array size
while (i < size)
{
if (arr[location] == arr[i])
{
// When get smallest element
counter += 1;
}
i += 1;
}
// Display the number of frequency
print(" Smallest element", arr[location] ,"are exists of ", counter ," times ");
}
}
func main()
{
let task: Frequency = Frequency();
// Define array of integer elements
let arr: [Int] = [2, 5, 1, 2, 6, 4, 1, 6, 7, 3, 1];
// Get the size
let size: Int = arr.count;
task.frequencyOfSmallest(arr, size);
}
main();
Output
Given Array
2 5 1 2 6 4 1 6 7 3 1
Smallest element 1 are exists of 3 times
/*
Kotlin Program for
Find frequency of smallest element in an array
*/
class Frequency
{
//Function which is display array elements
fun display(arr: Array < Int > , size: Int): Unit
{
var i: Int = 0;
while (i < size)
{
print(" " + arr[i]);
i += 1;
}
print("\n");
}
// Returns the location of smallest element of array
fun findSmallest(arr: Array < Int > , size: Int): Int
{
var location: Int = 0;
var i: Int = 1;
// Find smallest element
while (i < size)
{
if (arr[location] > arr[i])
{
location = i;
}
i += 1;
}
return location;
}
// Count frequency of smallest element in given array
fun frequencyOfSmallest(arr: Array < Int > , size: Int): Unit
{
// When have less than 2 elements
if (size <= 1)
{
return;
}
// Display array elements
print("\n Given Array \n");
this.display(arr, size);
// Find the location of smallest element
var location: Int = this.findSmallest(arr, size);
// Define counter variable
var counter: Int = 0;
var i: Int = 0;
// Execute loop through by array size
while (i < size)
{
if (arr[location] == arr[i])
{
// When get smallest element
counter += 1;
}
i += 1;
}
// Display the number of frequency
print(" Smallest element " + arr[location] + " are exists of " + counter + " times \n");
}
}
fun main(args: Array < String > ): Unit
{
var task: Frequency = Frequency();
// Define array of integer elements
var arr: Array < Int > = arrayOf(2, 5, 1, 2, 6, 4, 1, 6, 7, 3, 1);
// Get the size
var size: Int = arr.count();
task.frequencyOfSmallest(arr, size);
}
Output
Given Array
2 5 1 2 6 4 1 6 7 3 1
Smallest element 1 are exists of 3 times
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment