Find the all elements that appears n times
Here given code implementation process.
// C Program
// Find the all elements that appears n times
#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]);
}
}
// Display the all elements which is occurs of n times in given collection
void find_element(int arr[], int n, int size)
{
if (n <= 0)
{
return;
}
// Display element
display(arr, size);
// Loop controlling variables
int i;
int j;
// Define counter variable
int counter = 0;
int status = 0;
printf("\n Occurring elements of %d times is : ", n);
// Outer loop
for (i = 0; i < size; ++i)
{
// Inner loop
for (j = 0; j < size && counter >= 0 && counter <= n; ++j)
{
if (arr[i] == arr[j])
{
if (j < i)
{
// If element has been already tested
counter = -1;
}
else
{
counter++;
}
}
}
if (counter == n)
{ // When get new element
status = 1;
printf(" %d", arr[i]);
}
counter = 0;
}
if (status == 0)
{
printf("None");
}
printf("\n\n");
}
int main()
{
int arr[] = {
2 , 7 , 1 , 8 , 3 , 0 , 4 , 1 , 4 , 3 , 6 , 2 , 5 , 6 , 1 , 2 , 3 , 6
};
int size = sizeof(arr) / sizeof(arr[0]);
int n = 3;
find_element(arr, n, size);
return 0;
}
Output
2 7 1 8 3 0 4 1 4 3 6 2 5 6 1 2 3 6
Occurring elements of 3 times is : 2 1 3 6
/*
Java Program
Find the all elements that appears n times
*/
public class SearchElement
{
//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]);
}
}
// Display the all elements which is occurs of n times in given collection
public void find_element(int[] arr, int n, int size)
{
if (n <= 0)
{
return;
}
// Display element
display(arr, size);
// Loop controlling variables
int i;
int j;
// Define counter variable
int counter = 0;
boolean status = false;
System.out.print("\n Occurring elements of " + n + " times is : ");
// Outer loop
for (i = 0; i < size; ++i)
{
// Inner loop
for (j = 0; j < size && counter >= 0 && counter <= n; ++j)
{
if (arr[i] == arr[j])
{
if (j < i)
{
// If element has been already tested
counter = -1;
}
else
{
counter++;
}
}
}
if (counter == n)
{
// When get new element
status = true;
System.out.print(" " + arr[i]);
}
counter = 0;
}
if (status == false)
{
System.out.print("None");
}
System.out.print("\n\n");
}
public static void main(String[] args)
{
SearchElement s = new SearchElement();
int[] arr = {
2 , 7 , 1 , 8 , 3 , 0 , 4 , 1 , 4 , 3 , 6 , 2 , 5 , 6 , 1 , 2 , 3 , 6
};
// Get the size
int size = arr.length;
int n = 3;
s.find_element(arr, n, size);
}
}
Output
2 7 1 8 3 0 4 1 4 3 6 2 5 6 1 2 3 6
Occurring elements of 3 times is : 2 1 3 6
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Find the all elements that appears n times
*/
class SearchElement
{
public:
//Function which is display array elements
void display(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << " " << arr[i];
}
}
// Display the all elements which is occurs of n times in given collection
void find_element(int arr[], int n, int size)
{
if (n <= 0)
{
return;
}
// Display element
this->display(arr, size);
// Loop controlling variables
int i;
int j;
// Define counter variable
int counter = 0;
bool status = false;
cout << "\n Occurring elements of " << n << " times is : ";
// Outer loop
for (i = 0; i < size; ++i)
{
// Inner loop
for (j = 0; j < size && counter >= 0 && counter <= n; ++j)
{
if (arr[i] == arr[j])
{
if (j < i)
{
// If element has been already tested
counter = -1;
}
else
{
counter++;
}
}
}
if (counter == n)
{
// When get new element
status = true;
cout << " " << arr[i];
}
counter = 0;
}
if (status == false)
{
cout << "None";
}
cout << "\n\n";
}
};
int main()
{
SearchElement s = SearchElement();
int arr[] = {
2 , 7 , 1 , 8 , 3 , 0 , 4 , 1 , 4 , 3 , 6 , 2 , 5 , 6 , 1 , 2 , 3 , 6
};
// Get the size
int size = sizeof(arr) / sizeof(arr[0]);
int n = 3;
s.find_element(arr, n, size);
return 0;
}
Output
2 7 1 8 3 0 4 1 4 3 6 2 5 6 1 2 3 6
Occurring elements of 3 times is : 2 1 3 6
// Include namespace system
using System;
/*
C# Program
Find the all elements that appears n times
*/
public class SearchElement
{
//Function which is display array elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write(" " + arr[i]);
}
}
// Display the all elements which is occurs of n times in given collection
public void find_element(int[] arr, int n, int size)
{
if (n <= 0)
{
return;
}
// Display element
display(arr, size);
// Loop controlling variables
int i;
int j;
// Define counter variable
int counter = 0;
Boolean status = false;
Console.Write("\n Occurring elements of " + n + " times is : ");
// Outer loop
for (i = 0; i < size; ++i)
{
// Inner loop
for (j = 0; j < size && counter >= 0 && counter <= n; ++j)
{
if (arr[i] == arr[j])
{
if (j < i)
{
// If element has been already tested
counter = -1;
}
else
{
counter++;
}
}
}
if (counter == n)
{
// When get new element
status = true;
Console.Write(" " + arr[i]);
}
counter = 0;
}
if (status == false)
{
Console.Write("None");
}
Console.Write("\n\n");
}
public static void Main(String[] args)
{
SearchElement s = new SearchElement();
int[] arr = {
2 , 7 , 1 , 8 , 3 , 0 , 4 , 1 , 4 , 3 , 6 , 2 , 5 , 6 , 1 , 2 , 3 , 6
};
// Get the size
int size = arr.Length;
int n = 3;
s.find_element(arr, n, size);
}
}
Output
2 7 1 8 3 0 4 1 4 3 6 2 5 6 1 2 3 6
Occurring elements of 3 times is : 2 1 3 6
<?php
/*
Php Program
Find the all elements that appears n times
*/
class SearchElement
{
//Function which is display array elements
public function display( & $arr, $size)
{
for ($i = 0; $i < $size; ++$i)
{
echo " ". $arr[$i];
}
}
// Display the all elements which is occurs of n times in given collection
public function find_element( & $arr, $n, $size)
{
if ($n <= 0)
{
return;
}
// Display element
$this->display($arr, $size);
// Loop controlling variables
$i = 0;
$j = 0;
// Define counter variable
$counter = 0;
$status = false;
echo "\n Occurring elements of ". $n ." times is : ";
// Outer loop
for ($i = 0; $i < $size; ++$i)
{
// Inner loop
for ($j = 0; $j < $size && $counter >= 0 && $counter <= $n; ++$j)
{
if ($arr[$i] == $arr[$j])
{
if ($j < $i)
{
// If element has been already tested
$counter = -1;
}
else
{
$counter++;
}
}
}
if ($counter == $n)
{
// When get new element
$status = true;
echo " ". $arr[$i];
}
$counter = 0;
}
if ($status == false)
{
echo "None";
}
echo "\n\n";
}
}
function main()
{
$s = new SearchElement();
$arr = array(2, 7, 1, 8, 3, 0, 4, 1, 4, 3, 6, 2, 5, 6, 1, 2, 3, 6);
// Get the size
$size = count($arr);
$n = 3;
$s->find_element($arr, $n, $size);
}
main();
Output
2 7 1 8 3 0 4 1 4 3 6 2 5 6 1 2 3 6
Occurring elements of 3 times is : 2 1 3 6
/*
Node Js Program
Find the all elements that appears n times
*/
class SearchElement
{
//Function which is display array elements
display(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write(" " + arr[i]);
}
}
// Display the all elements which is occurs of n times in given collection
find_element(arr, n, size)
{
if (n <= 0)
{
return;
}
// Display element
this.display(arr, size);
// Loop controlling variables
var i = 0;
var j = 0;
// Define counter variable
var counter = 0;
var status = false;
process.stdout.write("\n Occurring elements of " + n + " times is : ");
// Outer loop
for (i = 0; i < size; ++i)
{
// Inner loop
for (j = 0; j < size && counter >= 0 && counter <= n; ++j)
{
if (arr[i] == arr[j])
{
if (j < i)
{
// If element has been already tested
counter = -1;
}
else
{
counter++;
}
}
}
if (counter == n)
{
// When get new element
status = true;
process.stdout.write(" " + arr[i]);
}
counter = 0;
}
if (status == false)
{
process.stdout.write("None");
}
process.stdout.write("\n\n");
}
}
function main()
{
var s = new SearchElement();
var arr = [2, 7, 1, 8, 3, 0, 4, 1, 4, 3, 6, 2, 5, 6, 1, 2, 3, 6];
// Get the size
var size = arr.length;
var n = 3;
s.find_element(arr, n, size);
}
main();
Output
2 7 1 8 3 0 4 1 4 3 6 2 5 6 1 2 3 6
Occurring elements of 3 times is : 2 1 3 6
# Python 3 Program
# Find the all elements that appears n times
class SearchElement :
# Function which is display array elements
def display(self, arr, size) :
i = 0
while (i < size) :
print(" ", arr[i], end = "")
i += 1
# Display the all elements which is occurs of n times in given collection
def find_element(self, arr, n, size) :
if (n <= 0) :
return
# Display element
self.display(arr, size)
# Loop controlling variables
i = 0
j = 0
# Define counter variable
counter = 0
status = False
print("\n Occurring elements of ", n ," times is : ", end = "")
# Outer loop
while (i < size) :
# Inner loop
j = 0
while (j < size and counter >= 0 and counter <= n) :
if (arr[i] == arr[j]) :
if (j < i) :
# If element has been already tested
counter = -1
else :
counter += 1
j += 1
if (counter == n) :
# When get new element
status = True
print(" ", arr[i], end = "")
counter = 0
i += 1
if (status == False) :
print("None", end = "")
print("\n")
def main() :
s = SearchElement()
arr = [2, 7, 1, 8, 3, 0, 4, 1, 4, 3, 6, 2, 5, 6, 1, 2, 3, 6]
# Get the size
size = len(arr)
n = 3
s.find_element(arr, n, size)
if __name__ == "__main__": main()
Output
2 7 1 8 3 0 4 1 4 3 6 2 5 6 1 2 3 6
Occurring elements of 3 times is : 2 1 3 6
# Ruby Program
# Find the all elements that appears n times
class SearchElement
# Function which is display array elements
def display(arr, size)
i = 0
while (i < size)
print(" ", arr[i])
i += 1
end
end
# Display the all elements which is occurs of n times in given collection
def find_element(arr, n, size)
if (n <= 0)
return
end
# Display element
self.display(arr, size)
# Loop controlling variables
i = 0
j = 0
# Define counter variable
counter = 0
status = false
print("\n Occurring elements of ", n ," times is : ")
# Outer loop
while (i < size)
# Inner loop
j = 0
while (j < size && counter >= 0 && counter <= n)
if (arr[i] == arr[j])
if (j < i)
# If element has been already tested
counter = -1
else
counter += 1
end
end
j += 1
end
if (counter == n)
# When get new element
status = true
print(" ", arr[i])
end
counter = 0
i += 1
end
if (status == false)
print("None")
end
print("\n\n")
end
end
def main()
s = SearchElement.new()
arr = [2, 7, 1, 8, 3, 0, 4, 1, 4, 3, 6, 2, 5, 6, 1, 2, 3, 6]
# Get the size
size = arr.length
n = 3
s.find_element(arr, n, size)
end
main()
Output
2 7 1 8 3 0 4 1 4 3 6 2 5 6 1 2 3 6
Occurring elements of 3 times is : 2 1 3 6
/*
Scala Program
Find the all elements that appears n times
*/
class SearchElement
{
//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;
}
}
// Display the all elements which is occurs of n times in given collection
def find_element(arr: Array[Int], n: Int, size: Int): Unit = {
if (n <= 0)
{
return;
}
// Display element
this.display(arr, size);
// Loop controlling variables
var i: Int = 0;
var j: Int = 0;
// Define counter variable
var counter: Int = 0;
var status: Boolean = false;
print("\n Occurring elements of " + n + " times is : ");
// Outer loop
while (i < size)
{
// Inner loop
j = 0;
while (j < size && counter >= 0 && counter <= n)
{
if (arr(i) == arr(j))
{
if (j < i)
{
// If element has been already tested
counter = -1;
}
else
{
counter += 1;
}
}
j += 1;
}
if (counter == n)
{
// When get new element
status = true;
print(" " + arr(i));
}
counter = 0;
i += 1;
}
if (status == false)
{
print("None");
}
print("\n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var s: SearchElement = new SearchElement();
var arr: Array[Int] = Array(2, 7, 1, 8, 3, 0, 4, 1, 4, 3, 6, 2, 5, 6, 1, 2, 3, 6);
// Get the size
var size: Int = arr.length;
var n: Int = 3;
s.find_element(arr, n, size);
}
}
Output
2 7 1 8 3 0 4 1 4 3 6 2 5 6 1 2 3 6
Occurring elements of 3 times is : 2 1 3 6
/*
Swift 4 Program
Find the all elements that appears n times
*/
class SearchElement
{
//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;
}
}
// Display the all elements which is occurs of n times in given collection
func find_element(_ arr: [Int], _ n: Int, _ size: Int)
{
if (n <= 0)
{
return;
}
// Display element
self.display(arr, size);
// Loop controlling variables
var i: Int = 0;
var j: Int = 0;
// Define counter variable
var counter: Int = 0;
var status: Bool = false;
print("\n Occurring elements of ", n ," times is : ", terminator: "");
// Outer loop
while (i < size)
{
// Inner loop
j = 0;
while (j < size && counter >= 0 && counter <= n)
{
if (arr[i] == arr[j])
{
if (j < i)
{
// If element has been already tested
counter = -1;
}
else
{
counter += 1;
}
}
j += 1;
}
if (counter == n)
{
// When get new element
status = true;
print(" ", arr[i], terminator: "");
}
counter = 0;
i += 1;
}
if (status == false)
{
print("None", terminator: "");
}
print("\n");
}
}
func main()
{
let s: SearchElement = SearchElement();
let arr: [Int] = [2, 7, 1, 8, 3, 0, 4, 1, 4, 3, 6, 2, 5, 6, 1, 2, 3, 6];
// Get the size
let size: Int = arr.count;
let n: Int = 3;
s.find_element(arr, n, size);
}
main();
Output
2 7 1 8 3 0 4 1 4 3 6 2 5 6 1 2 3 6
Occurring elements of 3 times is : 2 1 3 6
/*
Kotlin Program
Find the all elements that appears n times
*/
class SearchElement
{
//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;
}
}
// Display the all elements which is occurs of n times in given collection
fun find_element(arr: Array<Int>, n: Int, size: Int): Unit
{
if (n <= 0)
{
return;
}
// Display element
this.display(arr, size);
// Loop controlling variables
var i: Int = 0;
var j: Int ;
// Define counter variable
var counter: Int = 0;
var status: Boolean = false;
print("\n Occurring elements of " + n + " times is : ");
// Outer loop
while (i<size)
{
// Inner loop
j = 0;
while (j <size && counter>= 0 && counter <= n)
{
if (arr[i] == arr[j])
{
if (j<i)
{
// If element has been already tested
counter = -1;
}
else
{
counter += 1;
}
}
j += 1;
}
if (counter == n)
{
// When get new element
status = true;
print(" " + arr[i]);
}
counter = 0;
i += 1;
}
if (status == false)
{
print("None");
}
print("\n\n");
}
}
fun main(args: Array<String>): Unit
{
var s: SearchElement = SearchElement();
var arr: Array<Int> = arrayOf(2, 7, 1, 8, 3, 0, 4, 1, 4, 3, 6, 2, 5, 6, 1, 2, 3, 6);
// Get the size
var size: Int = arr.count();
var n: Int = 3;
s.find_element(arr, n, size);
}
Output
2 7 1 8 3 0 4 1 4 3 6 2 5 6 1 2 3 6
Occurring elements of 3 times is : 2 1 3 6
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