Posted on by Kalkicode
Code Mathematics

# Find most frequent element in an array by using sort

In an array, the most frequent element is the value that appears most frequently in the array. To find the most frequent element in an array using the sort() method, you would first sort the array in ascending order, and then count the occurrences of each element using a loop. The element with the highest frequency count would be considered the most frequent element in the array.

## Program Solution

``````/*
Java Program for
Find most frequent element in an array by using sort
*/
import java.util.Arrays;
public class Occurrence
{
// Display array elements
public void display(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
System.out.print(" " + arr[i]);
}
System.out.print("\n");
}
public void mostFrequentElement(int[] arr, int n)
{
// First sort given array elements
Arrays.sort(arr);
display(arr, n);
// Define some auxiliary variable
int result = arr[0];
int count = 1;
int max = 1;
// Execute the loop in range of 1..n
for (int i = 1; i < n; ++i)
{
if (arr[i] == arr[i - 1])
{
// When consecutive elements are similar
count++;
}
else
{
// Reset counter value
count = 1;
}
if (count > max)
{
// Get resultant element
result = arr[i - 1];
max = count;
}
}
System.out.println(" Result : " + result);
}
public static void main(String[] args)
{
int[] arr = {
6 , 6 , 2 , -9 , 1 , 2 , 2 , -9 , 2 , -9
};
int n = arr.length;
}
}``````

#### Output

`````` -9 -9 -9 1 2 2 2 2 6 6
Result : 2``````
``````// Include header file
#include <iostream>
#include <algorithm>
using namespace std;
/*
C++ Program for
Find most frequent element in an array by using sort
*/
class Occurrence
{
public:
// Display array elements
void display(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
cout << " " << arr[i];
}
cout << "\n";
}
void mostFrequentElement(int arr[], int n)
{
// First sort given array elements
sort(arr, arr + n);
this->display(arr, n);
// Define some auxiliary variable
int result = arr[0];
int count = 1;
int max = 1;
// Execute the loop in range of 1..n
for (int i = 1; i < n; ++i)
{
if (arr[i] == arr[i - 1])
{
// When consecutive elements are similar
count++;
}
else
{
// Reset counter value
count = 1;
}
if (count > max)
{
// Get resultant element
result = arr[i - 1];
max = count;
}
}
cout << " Result : " << result << endl;
}
};
int main()
{
int arr[] = {
6 , 6 , 2 , -9 , 1 , 2 , 2 , -9 , 2 , -9
};
int n = sizeof(arr) / sizeof(arr[0]);
return 0;
}``````

#### Output

`````` -9 -9 -9 1 2 2 2 2 6 6
Result : 2``````
``````// Include namespace system
using System;
/*
Csharp Program for
Find most frequent element in an array by using sort
*/
public class Occurrence
{
// Display array elements
public void display(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write(" " + arr[i]);
}
Console.Write("\n");
}
public void mostFrequentElement(int[] arr, int n)
{
// First sort given array elements
Array.Sort(arr);
this.display(arr, n);
// Define some auxiliary variable
int result = arr[0];
int count = 1;
int max = 1;
// Execute the loop in range of 1..n
for (int i = 1; i < n; ++i)
{
if (arr[i] == arr[i - 1])
{
// When consecutive elements are similar
count++;
}
else
{
// Reset counter value
count = 1;
}
if (count > max)
{
// Get resultant element
result = arr[i - 1];
max = count;
}
}
Console.WriteLine(" Result : " + result);
}
public static void Main(String[] args)
{
int[] arr = {
6 , 6 , 2 , -9 , 1 , 2 , 2 , -9 , 2 , -9
};
int n = arr.Length;
}
}``````

#### Output

`````` -9 -9 -9 1 2 2 2 2 6 6
Result : 2``````
``````package main
import "sort"
import "fmt"
/*
Go Program for
Find most frequent element in an array by using sort
*/

// Display array elements
func display(arr[] int, n int) {
for i := 0 ; i < n ; i++ {
fmt.Print(" ", arr[i])
}
fmt.Print("\n")
}
func mostFrequentElement(arr[] int, n int) {
// First sort given array elements
sort.Ints(arr)
display(arr, n)
// Define some auxiliary variable
var result int = arr[0]
var count int = 1
var max int = 1
// Execute the loop in range of 1..n
for i := 1 ; i < n ; i++ {
if arr[i] == arr[i - 1] {
// When consecutive elements are similar
count++
} else {
// Reset counter value
count = 1
}
if count > max {
// Get resultant element
result = arr[i - 1]
max = count
}
}
fmt.Println(" Result : ", result)
}
func main() {

var arr = [] int { 6 , 6 , 2 , -9 , 1 , 2 , 2 , -9 , 2 , -9 }
var n int = len(arr)
mostFrequentElement(arr, n)
}``````

#### Output

`````` -9 -9 -9 1 2 2 2 2 6 6
Result : 2``````
``````<?php
/*
Php Program for
Find most frequent element in an array by using sort
*/
class Occurrence
{
// Display array elements
public	function display(\$arr, \$n)
{
for (\$i = 0; \$i < \$n; ++\$i)
{
echo(" ".\$arr[\$i]);
}
echo("\n");
}
public	function mostFrequentElement(\$arr, \$n)
{
// First sort given array elements
sort(\$arr);
\$this->display(\$arr, \$n);
// Define some auxiliary variable
\$result = \$arr[0];
\$count = 1;
\$max = 1;
// Execute the loop in range of 1..n
for (\$i = 1; \$i < \$n; ++\$i)
{
if (\$arr[\$i] == \$arr[\$i - 1])
{
// When consecutive elements are similar
\$count++;
}
else
{
// Reset counter value
\$count = 1;
}
if (\$count > \$max)
{
// Get resultant element
\$result = \$arr[\$i - 1];
\$max = \$count;
}
}
echo(" Result : ".\$result."\n");
}
}

function main()
{
\$arr = array(6, 6, 2, -9, 1, 2, 2, -9, 2, -9);
\$n = count(\$arr);
}
main();``````

#### Output

`````` -9 -9 -9 1 2 2 2 2 6 6
Result : 2``````
``````/*
Node JS Program for
Find most frequent element in an array by using sort
*/
class Occurrence
{
// Display array elements
display(arr, n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write(" " + arr[i]);
}
process.stdout.write("\n");
}
mostFrequentElement(arr, n)
{
// First sort given array elements
arr.sort(function(a, b)
{
return a - b;
});
this.display(arr, n);
// Define some auxiliary variable
var result = arr[0];
var count = 1;
var max = 1;
// Execute the loop in range of 1..n
for (var i = 1; i < n; ++i)
{
if (arr[i] == arr[i - 1])
{
// When consecutive elements are similar
count++;
}
else
{
// Reset counter value
count = 1;
}
if (count > max)
{
// Get resultant element
result = arr[i - 1];
max = count;
}
}
console.log(" Result : " + result);
}
}

function main()
{
var arr = [6, 6, 2, -9, 1, 2, 2, -9, 2, -9];
var n = arr.length;
}
main();``````

#### Output

`````` -9 -9 -9 1 2 2 2 2 6 6
Result : 2``````
``````#   Python 3 Program for
#   Find most frequent element in an array by using sort
class Occurrence :
#  Display list elements
def display(self, arr, n) :
i = 0
while (i < n) :
print(" ", arr[i], end = "")
i += 1

print(end = "\n")

def mostFrequentElement(self, arr, n) :
#  First sort given list elements
arr.sort()
self.display(arr, n)
#  Define some auxiliary variable
result = arr[0]
count = 1
max = 1
i = 1
#  Execute the loop in range of 1..n
while (i < n) :
if (arr[i] == arr[i - 1]) :
#  When consecutive elements are similar
count += 1
else :
#  Reset counter value
count = 1

if (count > max) :
#  Get resultant element
result = arr[i - 1]
max = count

i += 1

print(" Result : ", result)

def main() :
arr = [6, 6, 2, -9, 1, 2, 2, -9, 2, -9]
n = len(arr)

if __name__ == "__main__": main()``````

#### Output

``````  -9  -9  -9  1  2  2  2  2  6  6
Result :  2``````
``````#   Ruby Program for
#   Find most frequent element in an array by using sort
class Occurrence
#  Display array elements
def display(arr, n)
i = 0
while (i < n)
print(" ", arr[i])
i += 1
end

print("\n")
end

def mostFrequentElement(arr, n)
#  First sort given array elements
arr = arr.sort
self.display(arr, n)
#  Define some auxiliary variable
result = arr[0]
count = 1
max = 1
i = 1
#  Execute the loop in range of 1..n
while (i < n)
if (arr[i] == arr[i - 1])
#  When consecutive elements are similar
count += 1
else

#  Reset counter value
count = 1
end

if (count > max)
#  Get resultant element
result = arr[i - 1]
max = count
end

i += 1
end

print(" Result : ", result, "\n")
end

end

def main()
arr = [6, 6, 2, -9, 1, 2, 2, -9, 2, -9]
n = arr.length
end

main()``````

#### Output

`````` -9 -9 -9 1 2 2 2 2 6 6
Result : 2
``````
``````import scala.collection.mutable._;
/*
Scala Program for
Find most frequent element in an array by using sort
*/
class Occurrence()
{
// Display array elements
def display(arr: Array[Int], n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print(" " + arr(i));
i += 1;
}
print("\n");
}
def mostFrequentElement(arr: Array[Int], n: Int): Unit = {
// First sort given array elements
scala.util.Sorting.quickSort(arr)
display(arr, n);
// Define some auxiliary variable
var result: Int = arr(0);
var count: Int = 1;
var max: Int = 1;
var i: Int = 1;
// Execute the loop in range of 1..n
while (i < n)
{
if (arr(i) == arr(i - 1))
{
// When consecutive elements are similar
count += 1;
}
else
{
// Reset counter value
count = 1;
}
if (count > max)
{
// Get resultant element
result = arr(i - 1);
max = count;
}
i += 1;
}
println(" Result : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Occurrence = new Occurrence();
var arr: Array[Int] = Array(6, 6, 2, -9, 1, 2, 2, -9, 2, -9);
var n: Int = arr.length;
}
}``````

#### Output

`````` -9 -9 -9 1 2 2 2 2 6 6
Result : 2``````
``````import Foundation;
/*
Swift 4 Program for
Find most frequent element in an array by using sort
*/
class Occurrence
{
// Display array elements
func display(_ arr: [Int], _ n: Int)
{
var i: Int = 0;
while (i < n)
{
print(" ", arr[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
func mostFrequentElement(_ arr: inout[Int], _ n: Int)
{
// First sort given array elements
arr = arr.sorted();
self.display(arr, n);
// Define some auxiliary variable
var result: Int = arr[0];
var count: Int = 1;
var max: Int = 1;
var i: Int = 1;
// Execute the loop in range of 1..n
while (i < n)
{
if (arr[i] == arr[i - 1])
{
// When consecutive elements are similar
count += 1;
}
else
{
// Reset counter value
count = 1;
}
if (count > max)
{
// Get resultant element
result = arr[i - 1];
max = count;
}
i += 1;
}
print(" Result : ", result);
}
}
func main()
{
var arr: [Int] = [6, 6, 2, -9, 1, 2, 2, -9, 2, -9];
let n: Int = arr.count;
}
main();``````

#### Output

``````  -9  -9  -9  1  2  2  2  2  6  6
Result :  2``````
``````/*
Kotlin Program for
Find most frequent element in an array by using sort
*/
class Occurrence
{
// Display array elements
fun display(arr: Array < Int > , n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print(" " + arr[i]);
i += 1;
}
print("\n");
}
fun mostFrequentElement(arr: Array < Int > , n: Int): Unit
{
// First sort given array elements
arr.sort();
this.display(arr, n);
// Define some auxiliary variable
var result: Int = arr[0];
var count: Int = 1;
var max: Int = 1;
var i: Int = 1;
// Execute the loop in range of 1..n
while (i < n)
{
if (arr[i] == arr[i - 1])
{
// When consecutive elements are similar
count += 1;
}
else
{
// Reset counter value
count = 1;
}
if (count > max)
{
// Get resultant element
result = arr[i - 1];
max = count;
}
i += 1;
}
println(" Result : " + result);
}
}
fun main(args: Array < String > ): Unit
{
val arr: Array < Int > = arrayOf(6, 6, 2, -9, 1, 2, 2, -9, 2, -9);
val n: Int = arr.count();
}``````

#### Output

`````` -9 -9 -9 1 2 2 2 2 6 6
Result : 2``````

## Comment

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.

Categories
Relative Post