Find the frequency of a element in array

Here given code implementation process.

// C Program 
// Find the frequency of a element in array
#include <stdio.h>

//Display elements of given array
void printArray(int arr[], int size)
{
	for (int i = 0; i < size; ++i)
	{
		printf("  %d", arr[i]);
	}
}
// Finding the frequency of a element (number) in an array
void findFrequency(int arr[], int size, int element)
{
	// Define resultant variable
	int counter = 0;
	// Execute loop through by size
	for (int i = 0; i < size; ++i)
	{
		if (arr[i] == element)
		{
			// When get element 
			counter++;
		}
	}
	// Display calculated result
	printf("\n Element %d frequency is : %d", element, counter);
}
int main(int argc, char const *argv[])
{
	// Define array of integer elements
	int arr[] = 
    {
		3 , 6 , 1 , 2 , 4 , 7 , 2 , 3 , 4 , 2 , 3 , 2 , 3 , 1 , 1 , 1 , 1
	};
	// Get the size
	int size = sizeof(arr) / sizeof(arr[0]);
	printf(" Array elements \n");
	printArray(arr, size);
	findFrequency(arr, size, 1);
	findFrequency(arr, size, 2);
	findFrequency(arr, size, 6);
	findFrequency(arr, size, 9);
	return 0;
}

Output

 Array elements
  3  6  1  2  4  7  2  3  4  2  3  2  3  1  1  1  1
 Element 1 frequency is : 5
 Element 2 frequency is : 4
 Element 6 frequency is : 1
 Element 9 frequency is : 0
/*
    Java Program
    Find the frequency of a element in array
*/
public class Frequency
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print("  " + arr[i]);
		}
	}
	// Finding the frequency of a element (number) in an array
	public void findFrequency(int[] arr, int size, int element)
	{
		// Define resultant variable
		int counter = 0;
		// Execute loop through by size
		for (int i = 0; i < size; ++i)
		{
			if (arr[i] == element)
			{
				// When get element 
				counter++;
			}
		}
		// Display calculated result
		System.out.print("\n Element " + element + " frequency is : " + counter);
	}
	public static void main(String[] args)
	{
		Frequency task = new Frequency();
		// Define array of integer elements
		int[] arr = 
        {
			3 , 6 , 1 , 2 , 4 , 7 , 2 , 3 , 4 , 2 , 3 , 2 , 3 , 1 , 1 , 1 , 1
		};
		// Get the size
		int size = arr.length;
		System.out.print(" Array elements \n");
		task.printArray(arr, size);
		task.findFrequency(arr, size, 1);
		task.findFrequency(arr, size, 2);
		task.findFrequency(arr, size, 6);
		task.findFrequency(arr, size, 9);
	}
}

Output

 Array elements
  3  6  1  2  4  7  2  3  4  2  3  2  3  1  1  1  1
 Element 1 frequency is : 5
 Element 2 frequency is : 4
 Element 6 frequency is : 1
 Element 9 frequency is : 0
// Include header file
#include <iostream>

using namespace std;
/*
    C++ Program
    Find the frequency of a element in array
*/
class Frequency
{
	public:
		//Display elements of given array
		void printArray(int arr[], int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << "  " << arr[i];
			}
		}
	// Finding the frequency of a element (number) in an array
	void findFrequency(int arr[], int size, int element)
	{
		// Define resultant variable
		int counter = 0;
		// Execute loop through by size
		for (int i = 0; i < size; ++i)
		{
			if (arr[i] == element)
			{
				// When get element
				counter++;
			}
		}
		// Display calculated result
		cout << "\n Element " << element << " frequency is : " << counter;
	}
};
int main()
{
	Frequency task = Frequency();
	// Define array of integer elements
	int arr[] = {
		3 , 6 , 1 , 2 , 4 , 7 , 2 , 3 , 4 , 2 , 3 , 2 , 3 , 1 , 1 , 1 , 1
	};
	// Get the size
	int size = sizeof(arr) / sizeof(arr[0]);
	cout << " Array elements \n";
	task.printArray(arr, size);
	task.findFrequency(arr, size, 1);
	task.findFrequency(arr, size, 2);
	task.findFrequency(arr, size, 6);
	task.findFrequency(arr, size, 9);
	return 0;
}

Output

 Array elements
  3  6  1  2  4  7  2  3  4  2  3  2  3  1  1  1  1
 Element 1 frequency is : 5
 Element 2 frequency is : 4
 Element 6 frequency is : 1
 Element 9 frequency is : 0
// Include namespace system
using System;
/*
    C# Program
    Find the frequency of a element in array
*/
public class Frequency
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			Console.Write("  " + arr[i]);
		}
	}
	// Finding the frequency of a element (number) in an array
	public void findFrequency(int[] arr, int size, int element)
	{
		// Define resultant variable
		int counter = 0;
		// Execute loop through by size
		for (int i = 0; i < size; ++i)
		{
			if (arr[i] == element)
			{
				// When get element
				counter++;
			}
		}
		// Display calculated result
		Console.Write("\n Element " + element + " frequency is : " + counter);
	}
	public static void Main(String[] args)
	{
		Frequency task = new Frequency();
		// Define array of integer elements
		int[] arr = {
			3 , 6 , 1 , 2 , 4 , 7 , 2 , 3 , 4 , 2 , 3 , 2 , 3 , 1 , 1 , 1 , 1
		};
		// Get the size
		int size = arr.Length;
		Console.Write(" Array elements \n");
		task.printArray(arr, size);
		task.findFrequency(arr, size, 1);
		task.findFrequency(arr, size, 2);
		task.findFrequency(arr, size, 6);
		task.findFrequency(arr, size, 9);
	}
}

Output

 Array elements
  3  6  1  2  4  7  2  3  4  2  3  2  3  1  1  1  1
 Element 1 frequency is : 5
 Element 2 frequency is : 4
 Element 6 frequency is : 1
 Element 9 frequency is : 0
<?php
/*
    Php Program
    Find the frequency of a element in array
*/
class Frequency
{
	//Display elements of given array
	public	function printArray( & $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo "  ". $arr[$i];
		}
	}
	// Finding the frequency of a element (number) in an array
	public	function findFrequency( & $arr, $size, $element)
	{
		// Define resultant variable
		$counter = 0;
		// Execute loop through by size
		for ($i = 0; $i < $size; ++$i)
		{
			if ($arr[$i] == $element)
			{
				// When get element
				$counter++;
			}
		}
		// Display calculated result
		echo "\n Element ". $element ." frequency is : ". $counter;
	}
}

function main()
{
	$task = new Frequency();
	// Define array of integer elements
	$arr = array(3, 6, 1, 2, 4, 7, 2, 3, 4, 2, 3, 2, 3, 1, 1, 1, 1);
	// Get the size
	$size = count($arr);
	echo " Array elements \n";
	$task->printArray($arr, $size);
	$task->findFrequency($arr, $size, 1);
	$task->findFrequency($arr, $size, 2);
	$task->findFrequency($arr, $size, 6);
	$task->findFrequency($arr, $size, 9);
}
main();

Output

 Array elements
  3  6  1  2  4  7  2  3  4  2  3  2  3  1  1  1  1
 Element 1 frequency is : 5
 Element 2 frequency is : 4
 Element 6 frequency is : 1
 Element 9 frequency is : 0
/*
    Node Js Program
    Find the frequency of a element in array
*/
class Frequency
{
	//Display elements of given array
	printArray(arr, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write("  " + arr[i]);
		}
	}
	// Finding the frequency of a element (number) in an array
	findFrequency(arr, size, element)
	{
		// Define resultant variable
		var counter = 0;
		// Execute loop through by size
		for (var i = 0; i < size; ++i)
		{
			if (arr[i] == element)
			{
				// When get element
				counter++;
			}
		}
		// Display calculated result
		process.stdout.write("\n Element " + element + " frequency is : " + counter);
	}
}

function main()
{
	var task = new Frequency();
	// Define array of integer elements
	var arr = [3, 6, 1, 2, 4, 7, 2, 3, 4, 2, 3, 2, 3, 1, 1, 1, 1];
	// Get the size
	var size = arr.length;
	process.stdout.write(" Array elements \n");
	task.printArray(arr, size);
	task.findFrequency(arr, size, 1);
	task.findFrequency(arr, size, 2);
	task.findFrequency(arr, size, 6);
	task.findFrequency(arr, size, 9);
}
main();

Output

 Array elements
  3  6  1  2  4  7  2  3  4  2  3  2  3  1  1  1  1
 Element 1 frequency is : 5
 Element 2 frequency is : 4
 Element 6 frequency is : 1
 Element 9 frequency is : 0
#  Python 3 Program
#  Find the frequency of a element in array

class Frequency :
	# Display elements of given array
	def printArray(self, arr, size) :
		i = 0
		while (i < size) :
			print("  ", arr[i], end = "")
			i += 1
		
	
	#  Finding the frequency of a element (number) in an array
	def findFrequency(self, arr, size, element) :
		#  Define resultant variable
		counter = 0
		i = 0
		#  Execute loop through by size
		while (i < size) :
			if (arr[i] == element) :
				#  When get element
				counter += 1
			
			i += 1
		
		#  Display calculated result
		print("\n Element ", element ," frequency is : ", counter, end = "")
	

def main() :
	task = Frequency()
	#  Define array of integer elements
	arr = [3, 6, 1, 2, 4, 7, 2, 3, 4, 2, 3, 2, 3, 1, 1, 1, 1]
	#  Get the size
	size = len(arr)
	print(" Array elements ")
	task.printArray(arr, size)
	task.findFrequency(arr, size, 1)
	task.findFrequency(arr, size, 2)
	task.findFrequency(arr, size, 6)
	task.findFrequency(arr, size, 9)

if __name__ == "__main__": main()

Output

 Array elements
   3   6   1   2   4   7   2   3   4   2   3   2   3   1   1   1   1
 Element  1  frequency is :  5
 Element  2  frequency is :  4
 Element  6  frequency is :  1
 Element  9  frequency is :  0
#  Ruby Program
#  Find the frequency of a element in array

class Frequency 
	# Display elements of given array
	def printArray(arr, size) 
		i = 0
		while (i < size) 
			print("  ", arr[i])
			i += 1
		end

	end

	#  Finding the frequency of a element (number) in an array
	def findFrequency(arr, size, element) 
		#  Define resultant variable
		counter = 0
		i = 0
		#  Execute loop through by size
		while (i < size) 
			if (arr[i] == element) 
				#  When get element
				counter += 1
			end

			i += 1
		end

		#  Display calculated result
		print("\n Element ", element ," frequency is : ", counter)
	end

end

def main() 
	task = Frequency.new()
	#  Define array of integer elements
	arr = [3, 6, 1, 2, 4, 7, 2, 3, 4, 2, 3, 2, 3, 1, 1, 1, 1]
	#  Get the size
	size = arr.length
	print(" Array elements \n")
	task.printArray(arr, size)
	task.findFrequency(arr, size, 1)
	task.findFrequency(arr, size, 2)
	task.findFrequency(arr, size, 6)
	task.findFrequency(arr, size, 9)
end

main()

Output

 Array elements 
  3  6  1  2  4  7  2  3  4  2  3  2  3  1  1  1  1
 Element 1 frequency is : 5
 Element 2 frequency is : 4
 Element 6 frequency is : 1
 Element 9 frequency is : 0
/*
    Scala Program
    Find the frequency of a element in array
*/
class Frequency
{
	//Display elements of given array
	def printArray(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print("  " + arr(i));
			i += 1;
		}
	}
	// Finding the frequency of a element (number) in an array
	def findFrequency(arr: Array[Int], size: Int, element: Int): Unit = {
		// Define resultant variable
		var counter: Int = 0;
		var i: Int = 0;
		// Execute loop through by size
		while (i < size)
		{
			if (arr(i) == element)
			{
				// When get element
				counter += 1;
			}
			i += 1;
		}
		// Display calculated result
		print("\n Element " + element + " frequency is : " + counter);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Frequency = new Frequency();
		// Define array of integer elements
		var arr: Array[Int] = Array(3, 6, 1, 2, 4, 7, 2, 3, 4, 2, 3, 2, 3, 1, 1, 1, 1);
		// Get the size
		var size: Int = arr.length;
		print(" Array elements \n");
		task.printArray(arr, size);
		task.findFrequency(arr, size, 1);
		task.findFrequency(arr, size, 2);
		task.findFrequency(arr, size, 6);
		task.findFrequency(arr, size, 9);
	}
}

Output

 Array elements
  3  6  1  2  4  7  2  3  4  2  3  2  3  1  1  1  1
 Element 1 frequency is : 5
 Element 2 frequency is : 4
 Element 6 frequency is : 1
 Element 9 frequency is : 0
/*
    Swift 4 Program
    Find the frequency of a element in array
*/
class Frequency
{
	//Display elements of given array
	func printArray(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print("  ", arr[i], terminator: "");
			i += 1;
		}
	}
	// Finding the frequency of a element (number) in an array
	func findFrequency(_ arr: [Int], _ size: Int, _ element: Int)
	{
		// Define resultant variable
		var counter: Int = 0;
		var i: Int = 0;
		// Execute loop through by size
		while (i < size)
		{
			if (arr[i] == element)
			{
				// When get element
				counter += 1;
			}
			i += 1;
		}
		// Display calculated result
		print("\n Element ", element ," frequency is : ", counter, terminator: "");
	}
}
func main()
{
	let task: Frequency = Frequency();
	// Define array of integer elements
	let arr: [Int] = [3, 6, 1, 2, 4, 7, 2, 3, 4, 2, 3, 2, 3, 1, 1, 1, 1];
	// Get the size
	let size: Int = arr.count;
	print(" Array elements ");
	task.printArray(arr, size);
	task.findFrequency(arr, size, 1);
	task.findFrequency(arr, size, 2);
	task.findFrequency(arr, size, 6);
	task.findFrequency(arr, size, 9);
}
main();

Output

 Array elements
   3   6   1   2   4   7   2   3   4   2   3   2   3   1   1   1   1
 Element  1  frequency is :  5
 Element  2  frequency is :  4
 Element  6  frequency is :  1
 Element  9  frequency is :  0
/*
    Kotlin Program
    Find the frequency of a element in array
*/
class Frequency
{
	//Display elements of given array
	fun printArray(arr: Array < Int > , size: Int): Unit
	{
		var i: Int = 0;
		while (i < size)
		{
			print("  " + arr[i]);
			i += 1;
		}
	}
	// Finding the frequency of a element (number) in an array
	fun findFrequency(arr: Array < Int > , size: Int, element: Int): Unit
	{
		// Define resultant variable
		var counter: Int = 0;
		var i: Int = 0;
		// Execute loop through by size
		while (i < size)
		{
			if (arr[i] == element)
			{
				// When get element
				counter += 1;
			}
			i += 1;
		}
		// Display calculated result
		print("\n Element " + element + " frequency is : " + counter);
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Frequency = Frequency();
	// Define array of integer elements
	var arr: Array < Int > = arrayOf(3, 6, 1, 2, 4, 7, 2, 3, 4, 2, 3, 2, 3, 1, 1, 1, 1);
	// Get the size
	var size: Int = arr.count();
	print(" Array elements \n");
	task.printArray(arr, size);
	task.findFrequency(arr, size, 1);
	task.findFrequency(arr, size, 2);
	task.findFrequency(arr, size, 6);
	task.findFrequency(arr, size, 9);
}

Output

 Array elements
  3  6  1  2  4  7  2  3  4  2  3  2  3  1  1  1  1
 Element 1 frequency is : 5
 Element 2 frequency is : 4
 Element 6 frequency is : 1
 Element 9 frequency is : 0


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







© 2021, kalkicode.com, All rights reserved