# Group the array elements by occurrence sequence

Here given code implementation process.

``````/*
Java program
Group the array elements by occurrence sequence
*/
import java.util.HashMap;
public class Grouping
{
// Function which is 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");
}
// Combine group element
public void combining(int []arr, int n)
{
System.out.print("\n Given Array \n");
// Display given array
display(arr, n);
HashMap < Integer, Integer > map = new HashMap < Integer, Integer > ();
int[] result = new int[n];
// Loop controlling variable
int i = 0;
int j = 0;
int index = 0;
// iterate the loop through by size
for (i = 0; i < n; ++i)
{
if (map.containsKey(arr[i]))
{
// When key exists then update value
map.put(arr[i], map.get(arr[i]) + 1);
}
else
{
map.put(arr[i], 1);
}
}
// iterate the loop through by size
for (i = 0; i < n; ++i)
{
if (map.containsKey(arr[i]))
{
// Get repeated element into resultant array
for (j = 0; j < map.get(arr[i]); ++j)
{
// Put element
result[index] = arr[i];
// next index location
index++;
}
// Remove current element
map.remove(arr[i]);
}
}
// Display calculated result
System.out.print(" Resultant Array \n");
display(result, n);
}
public static void main(String[] args)
{
// Define array of integer elements
int num[] = {
2 , 8 , 2 , 4 , 7 , 2 , 5 , 4 , 8 , 1
};
// Get the number of element
int n = num.length;
}
}``````

#### Output

`````` Given Array
2   8   2   4   7   2   5   4   8   1
Resultant Array
2   2   2   8   8   4   4   7   5   1``````
``````// Include header file
#include <iostream>
#include <unordered_map>
using namespace std;

class Grouping
{
public:
// Function which is display array elements
void display(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
cout << "   " << arr[i];
}
cout << "\n";
}
// Combine group element
void combining(int arr[], int n)
{
cout << "\n Given Array \n";
// Display given array
this->display(arr, n);
unordered_map < int, int > map;
int result[n];
// Loop controlling variable
int i = 0;
int j = 0;
int index = 0;
// iterate the loop through by size
for (i = 0; i < n; ++i)
{
if (map.find(arr[i]) != map.end())
{
map[arr[i]] = map[arr[i]] + 1;
}
else
{
map[arr[i]] = 1;
}
}
// iterate the loop through by size
for (i = 0; i < n; ++i)
{
if (map.find(arr[i]) != map.end())
{
// Get repeated element into resultant array
for (j = 0; j < map[arr[i]]; ++j)
{
// next index location
// Put element
result[index] = arr[i];
index++;
}
map.erase(arr[i]);
}
}
// Display calculated result
cout << " Resultant Array \n";
this->display(result, n);
}
};
int main()
{
// Define array of integer elements
int num[] = {
2 , 8 , 2 , 4 , 7 , 2 , 5 , 4 , 8 , 1
};
// Get the number of element
int n = sizeof(num) / sizeof(num[0]);
return 0;
}``````

#### Output

`````` Given Array
2   8   2   4   7   2   5   4   8   1
Resultant Array
2   2   2   8   8   4   4   7   5   1``````
``````// Include namespace system
using System;
using System.Collections.Generic;
public class Grouping
{
// Function which is display array elements
public void display(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write("   " + arr[i]);
}
Console.Write("\n");
}
// Combine group element
public void combining(int[] arr, int n)
{
Console.Write("\n Given Array \n");
// Display given array
display(arr, n);
Dictionary < int, int > map = new Dictionary < int, int > ();
int[] result = new int[n];
// Loop controlling variable
int i = 0;
int j = 0;
int index = 0;
// iterate the loop through by size
for (i = 0; i < n; ++i)
{
if (map.ContainsKey(arr[i]))
{
map[arr[i]] = map[arr[i]] + 1;
}
else
{
}
}
// iterate the loop through by size
for (i = 0; i < n; ++i)
{
if (map.ContainsKey(arr[i]))
{
// Get repeated element into resultant array
for (j = 0; j < map[arr[i]]; ++j)
{
// next index location
// Put element
result[index] = arr[i];
index++;
}
map.Remove(arr[i]);
}
}
// Display calculated result
Console.Write(" Resultant Array \n");
display(result, n);
}
public static void Main(String[] args)
{
// Define array of integer elements
int[] num = {
2 , 8 , 2 , 4 , 7 , 2 , 5 , 4 , 8 , 1
};
// Get the number of element
int n = num.Length;
}
}``````

#### Output

`````` Given Array
2   8   2   4   7   2   5   4   8   1
Resultant Array
2   2   2   8   8   4   4   7   5   1``````
``````<?php
class Grouping
{
// Function which is display array elements
public	function display( & \$arr, \$n)
{
for (\$i = 0; \$i < \$n; ++\$i)
{
echo "   ". \$arr[\$i];
}
echo "\n";
}
// Combine group element
public	function combining( & \$arr, \$n)
{
echo "\n Given Array \n";
// Display given array
\$this->display(\$arr, \$n);
\$map = array();
\$result = array_fill(0, \$n, 0);
// Loop controlling variable
\$i = 0;
\$j = 0;
\$index = 0;
// iterate the loop through by size
for (\$i = 0; \$i < \$n; ++\$i)
{
if (array_key_exists(\$arr[\$i], \$map))
{
\$map[\$arr[\$i]] = \$map[\$arr[\$i]] + 1;
}
else
{
\$map[\$arr[\$i]] = 1;
}
}
// iterate the loop through by size
for (\$i = 0; \$i < \$n; ++\$i)
{
if (array_key_exists(\$arr[\$i], \$map))
{
// Get repeated element into resultant array
for (\$j = 0; \$j < \$map[\$arr[\$i]]; ++\$j)
{
// next index location
// Put element
\$result[\$index] = \$arr[\$i];
\$index++;
}
unset(\$map[\$arr[\$i]]);
}
}
// Display calculated result
echo " Resultant Array \n";
\$this->display(\$result, \$n);
}
}

function main()
{
// Define array of integer elements
\$num = array(2, 8, 2, 4, 7, 2, 5, 4, 8, 1);
// Get the number of element
\$n = count(\$num);
}
main();``````

#### Output

`````` Given Array
2   8   2   4   7   2   5   4   8   1
Resultant Array
2   2   2   8   8   4   4   7   5   1``````
``````/*
Node Js program
Group the array elements by occurrence sequence
*/
class Grouping
{
// Function which is display array elements
display(arr, n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write("   " + arr[i]);
}
process.stdout.write("\n");
}
// Combine group element
combining(arr, n)
{
process.stdout.write("\n Given Array \n");
// Display given array
this.display(arr, n);
var map = new Map();
var result = Array(n).fill(0);
// Loop controlling variable
var i = 0;
var j = 0;
var index = 0;
// iterate the loop through by size
for (i = 0; i < n; ++i)
{
if (map.has(arr[i]))
{
map.set(arr[i], map.get(arr[i]) + 1);
}
else
{
map.set(arr[i], 1);
}
}
// iterate the loop through by size
for (i = 0; i < n; ++i)
{
if (map.has(arr[i]))
{
// Get repeated element into resultant array
for (j = 0; j < map.get(arr[i]); ++j)
{
// next index location
// Put element
result[index] = arr[i];
index++;
}
map.delete(arr[i]);
}
}
// Display calculated result
process.stdout.write(" Resultant Array \n");
this.display(result, n);
}
}

function main()
{
// Define array of integer elements
var num = [2, 8, 2, 4, 7, 2, 5, 4, 8, 1];
// Get the number of element
var n = num.length;
}
main();``````

#### Output

`````` Given Array
2   8   2   4   7   2   5   4   8   1
Resultant Array
2   2   2   8   8   4   4   7   5   1``````
``````#  Python 3 program
#  Group the array elements by occurrence sequence

class Grouping :
#  Function which is display list elements
def display(self, arr, n) :
i = 0
while (i < n) :
print("  ", arr[i], end = "")
i += 1

print(end = "\n")

#  Combine group element
def combining(self, arr, n) :
print("\n Given Array ")
#  Display given list
self.display(arr, n)
map = dict()
result = [0] * (n)
#  Loop controlling variable
i = 0
j = 0
index = 0
#  iterate the loop through by size
while (i < n) :
if (arr[i] in map.keys()) :
map[arr[i]] = map.get(arr[i]) + 1
else :
map[arr[i]] = 1

i += 1

#  iterate the loop through by size
i = 0
while (i < n) :
if (arr[i] in map.keys()) :
#  Get repeated element into resultant list
while (j < map.get(arr[i])) :
#  next index location
#  Put element
result[index] = arr[i]
index += 1
j += 1

del map[arr[i]]
j = 0

i += 1

#  Display calculated result
print(" Resultant Array ")
self.display(result, n)

def main() :
#  Define list of integer elements
num = [2, 8, 2, 4, 7, 2, 5, 4, 8, 1]
#  Get the number of element
n = len(num)

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

#### Output

`````` Given Array
2   8   2   4   7   2   5   4   8   1
Resultant Array
2   2   2   8   8   4   4   7   5   1``````
``````#  Ruby program
#  Group the array elements by occurrence sequence

class Grouping
#  Function which is display array elements
def display(arr, n)
i = 0
while (i < n)
print("   ", arr[i])
i += 1
end

print("\n")
end

#  Combine group element
def combining(arr, n)
print("\n Given Array \n")
#  Display given array
self.display(arr, n)
map = Hash.new
result = Array.new(n) {0}
#  Loop controlling variable
i = 0
j = 0
index = 0
#  iterate the loop through by size
while (i < n)
if (map.key?(arr[i]))
map[arr[i]] = map[arr[i]] + 1
else
map[arr[i]] = 1
end

i += 1
end

#  iterate the loop through by size
i = 0
while (i < n)
if (map.key?(arr[i]))
#  Get repeated element into resultant array
while (j < map[arr[i]])
#  next index location
#  Put element
result[index] = arr[i]
index += 1
j += 1
end

map.delete(arr[i])
j = 0
end

i += 1
end

#  Display calculated result
print(" Resultant Array \n")
self.display(result, n)
end

end

def main()
#  Define array of integer elements
num = [2, 8, 2, 4, 7, 2, 5, 4, 8, 1]
#  Get the number of element
n = num.length
end

main()``````

#### Output

`````` Given Array
2   8   2   4   7   2   5   4   8   1
Resultant Array
2   2   2   8   8   4   4   7   5   1
``````
``````import scala.collection.mutable._;
/*
Scala program
Group the array elements by occurrence sequence
*/
class Grouping
{
// Function which is 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");
}
// Combine group element
def combining(arr: Array[Int], n: Int): Unit = {
print("\n Given Array \n");
// Display given array
this.display(arr, n);
var map: Map[Int, Int] = Map();
var result: Array[Int] = Array.fill[Int](n)(0);
// Loop controlling variable
var i: Int = 0;
var j: Int = 0;
var index: Int = 0;
// iterate the loop through by size
while (i < n)
{
if (map.contains(arr(i)))
{
}
else
{
}
i += 1;
}
// iterate the loop through by size
i = 0;
while (i < n)
{
if (map.contains(arr(i)))
{
// Get repeated element into resultant array
while (j < map.get(arr(i)).get)
{
// next index location
// Put element
result(index) = arr(i);
index += 1;
j += 1;
}
map.remove(arr(i));
j = 0;
}
i += 1;
}
// Display calculated result
print(" Resultant Array \n");
this.display(result, n);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Grouping = new Grouping();
// Define array of integer elements
var num: Array[Int] = Array(2, 8, 2, 4, 7, 2, 5, 4, 8, 1);
// Get the number of element
var n: Int = num.length;
}
}``````

#### Output

`````` Given Array
2   8   2   4   7   2   5   4   8   1
Resultant Array
2   2   2   8   8   4   4   7   5   1``````
``````import Foundation
/*
Swift 4 program
Group the array elements by occurrence sequence
*/
class Grouping
{
// Function which is 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");
}
// Combine group element
func combining(_ arr: [Int], _ n: Int)
{
print("\n Given Array ");
// Display given array
self.display(arr, n);
// Define empty dictionary
var map = [Int: Int]();
var result: [Int] = Array(repeating: 0, count: n);
// Loop controlling variable
var i: Int = 0;
var j: Int = 0;
var index: Int = 0;
// iterate the loop through by size
while (i < n)
{
if (map.keys.contains(arr[i]))
{
map[arr[i]] = map[arr[i]]! + 1;
}
else
{
map[arr[i]] = 1;
}
i += 1;
}
// iterate the loop through by size
i = 0;
while (i < n)
{
if (map.keys.contains(arr[i]))
{
// Get repeated element into resultant array
while (j < map[arr[i]]!)
{
// next index location
// Put element
result[index] = arr[i];
index += 1;
j += 1;
}
map.removeValue(forKey: arr[i]);
j = 0;
}
i += 1;
}
// Display calculated result
print(" Resultant Array ");
self.display(result, n);
}
}
func main()
{
// Define array of integer elements
let num: [Int] = [2, 8, 2, 4, 7, 2, 5, 4, 8, 1];
// Get the number of element
let n: Int = num.count;
}
main();``````

#### Output

`````` Given Array
2   8   2   4   7   2   5   4   8   1
Resultant Array
2   2   2   8   8   4   4   7   5   1``````
``````/*
Kotlin program
Group the array elements by occurrence sequence
*/
class Grouping
{
// Function which is 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");
}
// Combine group element
fun combining(arr: Array < Int > , n: Int): Unit
{
print("\n Given Array \n");
// Display given array
this.display(arr, n);
var map = mutableMapOf<Int, Int>();
var result: Array < Int > = Array(n)
{
0
};
// Loop controlling variable
var i: Int = 0;
var j: Int = 0;
var index: Int = 0;
// iterate the loop through by size
while (i < n)
{
if (map.containsKey(arr[i]))
{
map.put(arr[i], map.getValue(arr[i]) + 1);
}
else
{
map.put(arr[i], 1);
}
i += 1;
}
// iterate the loop through by size
i = 0;
while (i < n)
{
if (map.containsKey(arr[i]))
{
// Get repeated element into resultant array
while (j < map.getValue(arr[i]))
{
// next index location
// Put element
result[index] = arr[i];
index += 1;
j += 1;
}
map.remove(arr[i]);
j = 0;
}
i += 1;
}
// Display calculated result
print(" Resultant Array \n");
this.display(result, n);
}
}
fun main(args: Array <String> ): Unit
{
// Define array of integer elements
var num: Array <Int> = arrayOf(2, 8, 2, 4, 7, 2, 5, 4, 8, 1);
// Get the number of element
var n: Int = num.count();
}``````

#### Output

`````` Given Array
2   8   2   4   7   2   5   4   8   1
Resultant Array
2   2   2   8   8   4   4   7   5   1``````

## 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.