Find the most repeated element in an array
Here given code implementation process.
// C++ Program
// Find the most repeated element in an array
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
//This Function finding most repeating element in given array
void most_occurring_item(int arr[], int size)
{
//Define a empty map
map < int, int > mp;
for (int i = 0; i < size; ++i)
{
// When key is already exists then incrementing the value by one
// Or when key are not exist first creating a new element and set 0 that value.
// And increment its value
mp[arr[i]]++;
}
map < int, int > ::iterator element;
// This variable are used to detect the status of
// Duplicate elements are exist in given collection
bool status = true;
int result = 0, counter = 0;
for (element = mp.begin(); element != mp.end(); ++element)
{
if (counter < element->second)
{
//When get a new most occuring element
counter = element->second;
result = element->first;
status = true;
}
}
if (status == true)
{
//When result are exist in given array collection
cout << result << endl;
}
else
{
cout << "\n No found repeated elements";
}
}
int main()
{
//Array element
int arr[] = {
1 , 5 , 4 , 2 , 7 , 9 , 1 , 5 , 4 , 2 , 5 , 7
};
//Get the size of array
int size = sizeof(arr) / sizeof(arr[0]);
most_occurring_item(arr, size);
return 0;
}
Output
5
/*
Java program
Find the most repeated element in an array
*/
import java.util.HashMap;
import java.util.Map;
public class Counting
{
// Find the element which is most frequent in an array
public void mostRepeated(int[] arr)
{
// This is used to collect result
int result = 0;
int counter = 1;
// Create a empty map
Map < Integer, Integer > map = new HashMap < > ();
// iterate through by array size
for (int i = 0; i < arr.length; i++)
{
if (map.containsKey(arr[i]))
{
// When key exists then update value
map.put(arr[i], map.get(arr[i]) + 1);
}
else
{
// When inserting new element
map.put(arr[i], 1);
}
}
// Find most repeated element
for (int key: map.keySet())
{
if (map.get(key) > counter)
{
counter = map.get(key);
// Get new result
result = key;
}
}
if (result != 1)
{
System.out.print(result);
}
else
{
System.out.print("\n No found repeated elements");
}
}
public static void main(String[] args)
{
Counting task = new Counting();
// Array element
int[] arr = {
1 , 5 , 4 , 2 , 7 , 9 , 1 , 5 , 4 , 2 , 5 , 7
};
task.mostRepeated(arr);
}
}
Output
5
// Include namespace system
using System;
using System.Collections.Generic;
public class Counting
{
// Find the element which is most frequent in an array
public void mostRepeated(int[] arr)
{
// This is used to collect result
int result = 0;
int counter = 1;
// Create a empty map
Dictionary < int, int > map = new Dictionary < int, int > ();
// iterate through by array size
for (int i = 0; i < arr.Length; i++)
{
if (map.ContainsKey(arr[i]))
{
map[arr[i]] += 1;
}
else
{
map.Add(arr[i], 1);
}
}
// Find most repeated element
foreach(KeyValuePair <int, int > entry in map)
{
if (map[entry.Key] > counter)
{
counter = entry.Value;
// Get new result
result = entry.Key;
}
}
if (result != 1)
{
Console.Write(result);
}
else
{
Console.Write("\n No found repeated elements");
}
}
public static void Main(String[] args)
{
Counting task = new Counting();
// Array element
int[] arr = {
1 , 5 , 4 , 2 , 7 , 9 , 1 , 5 , 4 , 2 , 5 , 7
};
task.mostRepeated(arr);
}
}
Output
5
<?php
/*
Php program
Find the most repeated element in an array
*/
class Counting
{
// Find the element which is most frequent in an array
public function mostRepeated( & $arr)
{
// This is used to collect result
$result = 0;
$counter = 1;
// Create a empty array
$map = [];
// iterate through by array size
for ($i = 0; $i < count($arr); $i++)
{
if (array_key_exists($arr[$i], $map))
{ // When key exists then update value
$map[$arr[$i]] = $map[$arr[$i]] + 1;
}
else
{ // When inserting new element
$map[$arr[$i]] = 1;
}
}
// Find most repeated element
foreach ($map as $key => $value)
{
if ($value > $counter)
{
$counter = $value;
// Get new result
$result = $key;
}
}
if ($result != 1)
{
echo $result;
}
else
{
echo "\n No found repeated elements";
}
}
}
function main()
{
$task = new Counting();
// Array element
$arr = array(1, 5, 4, 2, 7, 9, 1, 5, 4, 2, 5, 7);
$task->mostRepeated($arr);
}
main();
Output
5
/*
Node Js program
Find the most repeated element in an array
*/
class Counting
{
// Find the element which is most frequent in an array
mostRepeated(arr)
{
// This is used to collect result
var result = 0;
var counter = 1;
// Create a empty map
var map = new Map();
// iterate through by array size
for (var i = 0; i < arr.length; i++)
{
if (map.has(arr[i]))
{
map.set(arr[i], map.get(arr[i]) + 1);
}
else
{
map.set(arr[i], 1);
}
}
// Find most repeated element
for (const [key, value] of map)
{
if (value > counter)
{
counter = value;
// Get new result
result = key;
}
}
if (result != 1)
{
console.log(result);
}
else
{
console.log("\n No found repeated elements");
}
}
}
function main()
{
var task = new Counting();
// Array element
var arr = [1, 5, 4, 2, 7, 9, 1, 5, 4, 2, 5, 7];
task.mostRepeated(arr);
}
main();
Output
5
# Python 3 program
# Find the most repeated element in an array
class Counting :
# Find the element which is most frequent in an list
def mostRepeated(self, arr) :
# This is used to collect result
result = 0
counter = 1
# Create a empty dic
map = {}
i = 0
# iterate through by list size
while (i < len(arr)) :
if (arr[i] in map.keys()) :
# When key exists then update value
map[arr[i]] = map.get(arr[i]) + 1
else :
# When inserting new element
map[arr[i]] = 1
i += 1
# Find most repeated element
for key, value in map.items() :
if (value > counter) :
counter = value
# Get new result
result = key
if (result != 1) :
print(result, end = "")
else :
print("\n No found repeated elements", end = "")
def main() :
task = Counting()
# Array element
arr = [1, 5, 4, 2, 7, 9, 1, 5, 4, 2, 5, 7]
task.mostRepeated(arr)
if __name__ == "__main__": main()
Output
5
# Ruby program
# Find the most repeated element in an array
class Counting
# Find the element which is most frequent in an array
def mostRepeated(arr)
# This is used to collect result
result = 0
counter = 1
# Create a empty map
map = Hash.new
i = 0
# iterate through by array size
while (i < arr.length)
if (map.key?(arr[i]))
map[arr[i]] = map[arr[i]] + 1
else
map[arr[i]] = 1
end
i += 1
end
# Find most repeated element
map.each{ |key,value|
if (map[key] > counter)
counter = map[key]
# Get new result
result = key
end
}
if (result != 1)
print(result)
else
print("\n No found repeated elements")
end
end
end
def main()
task = Counting.new()
# Array element
arr = [1, 5, 4, 2, 7, 9, 1, 5, 4, 2, 5, 7]
task.mostRepeated(arr)
end
main()
Output
5
import scala.collection.mutable.Map;
/*
Scala program
Find the most repeated element in an array
*/
class Counting
{
// Find the element which is most frequent in an array
def mostRepeated(arr: Array[Int]): Unit = {
// This is used to collect result
var result: Int = 0;
var counter: Int = 1;
// Create a empty map
var map: Map[Int, Int] = Map.empty[Int, Int];
var i: Int = 0;
var data = 0;
// iterate through by array size
while (i < arr.length)
{
if (map.contains(arr(i)))
{
data = map.get(arr(i)).get;
map(arr(i)) = ( data + 1);
}
else
{
map(arr(i)) = 1;
}
i += 1;
}
// Find most repeated element
map.keys.foreach(key =>
{
if (map.get(key).get > counter)
{
counter = map.get(key).get;
// Get new result
result = key;
}
})
if (result != 1)
{
print(result);
}
else
{
print("\n No found repeated elements");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Counting = new Counting();
// Array element
var arr: Array[Int] = Array(1, 5, 4, 2, 7, 9, 1, 5, 4, 2, 5, 7);
task.mostRepeated(arr);
}
}
Output
5
/*
Swift 4 program
Find the most repeated element in an array
*/
class Counting
{
// Find the element which is most frequent in an array
func mostRepeated(_ arr: [Int])
{
// This is used to collect result
var result: Int = 0;
var counter: Int = 1;
// Create a empty dictionary
var map: [Int: Int] = [:];
var i: Int = 0;
// iterate through by array size
while (i < arr.count)
{
if (map.keys.contains(arr[i]))
{
// When key exists then update value
map[arr[i]] = map[arr[i]]! + 1;
}
else
{
// When inserting new element
map[arr[i]] = 1;
}
i += 1;
}
// Find most repeated element
for (key, value) in map
{
if (value > counter)
{
counter = value;
// Get new result
result = key;
}
}
if (result != 1)
{
print(result, terminator: "");
}
else
{
print("\n No found repeated elements", terminator: "");
}
}
}
func main()
{
let task: Counting = Counting();
// Array element
let arr: [Int] = [1, 5, 4, 2, 7, 9, 1, 5, 4, 2, 5, 7];
task.mostRepeated(arr);
}
main();
Output
5
/*
Kotlin program
Find the most repeated element in an array
*/
class Counting
{
// Find the element which is most frequent in an array
fun mostRepeated(arr: Array < Int > ): Unit
{
// This is used to collect result
var result: Int = 0;
var counter: Int = 1;
// Create a empty map
var map = mutableMapOf<Int, Int>();
var i: Int = 0;
// iterate through by array size
while (i < arr.count())
{
if (map.containsKey(arr[i]))
{
map.put(arr[i], map.getValue(arr[i]) + 1);
}
else
{
map.put(arr[i], 1);
}
i += 1;
}
// Find most repeated element
for( (key, value) in map )
{
if (value > counter)
{
counter = value;
// Get new result
result = key;
}
}
if (result != 1)
{
print(result);
}
else
{
print("\n No found repeated elements");
}
}
}
fun main(args: Array <String> ): Unit
{
var task: Counting = Counting();
// Array element
var arr: Array < Int > = arrayOf(1, 5, 4, 2, 7, 9, 1, 5, 4, 2, 5, 7);
task.mostRepeated(arr);
}
Output
5
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