Convert an array to reduced form

Here given code implementation process.

``````/*
Java Program
Convert an array to reduced form
*/
import java.util.HashMap;
import java.util.HashSet;
import java.util.Arrays;
public class Reduction
{
//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");
}
// Handles the request to get sorted order element position
public void convert(int[] arr, int n)
{
if (n <= 0)
{
return;
}
// Used to collecting the sorted order sequence
HashMap < Integer, HashSet < Integer > > record
= new HashMap < Integer, HashSet < Integer > > ();
// Loop controlling variable
int temp = 0;
int i = 0;
// Auxiliary array
int[] auxiliary = arr.clone();
// Sort array elements in ascending order
Arrays.sort(auxiliary);
// Get order of sorted elements in proper way
for (i = 0; i < n; ++i)
{
if (record.containsKey(auxiliary[i]))
{
// When element is repeated
}
else
{
record.put(auxiliary[i], new HashSet < Integer > ());
}
}
// Assign the calculated result to actual array
for (i = 0; i < n; ++i)
{
// Get sorted position
temp = record.get(arr[i]).iterator().next();
// Remove first element
record.get(arr[i]).remove(temp);
// Assign sort position
arr[i] = temp;
}
}
public static void main(String[] arg)
{
// Define array of integer elements
// Element duplicates are allowed
int[] arr = {
3 , 5 , 8 , 2 , 10 , 6 , 9 , 5 , 3
};
// Get the length of array
int n = arr.length;
System.out.print(" Before Convert \n ");
System.out.print(" After Convert \n ");
}
}``````

Output

`````` Before Convert
3   5   8   2   10   6   9   5   3
After Convert
1   3   6   0   8   5   7   4   2``````
``````// Include header file
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <set>

using namespace std;
/*
C++ Program
Convert an array to reduced form
*/
class Reduction
{
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";
}
// Handles the request to get sorted order element position
void convert(int arr[], int n)
{
if (n <= 0)
{
return;
}
// Used to collecting the sorted order sequence
unordered_map < int, set < int > > record;
// Loop controlling variable
int temp = 0;
int i = 0;
// Auxiliary array
int auxiliary[n];
// Get actual array element
for (i = 0; i < n; ++i)
{
auxiliary[i] = arr[i];
}
// Sort array elements in ascending order
sort(auxiliary, auxiliary+n);
// Get order of sorted elements in proper way
for (i = 0; i < n; ++i)
{
if (record.find(auxiliary[i]) != record.end())
{
// When element is repeated
record[auxiliary[i]].insert(i);
}
else
{
record[auxiliary[i]].insert(i);
}
}
// Assign the calculated result to actual array
for (i = 0; i < n; ++i)
{
// Get sorted position
temp = *record[arr[i]].begin();
// Remove first element
record[arr[i]].erase(temp);
// Assign sort position
arr[i] = temp;
}
}
};
int main()
{
// Define array of integer elements
// Element duplicates are allowed
int arr[] = {
3 , 5 , 8 , 2 , 10 , 6 , 9 , 5 , 3
};
// Get the length of array
int n = sizeof(arr) / sizeof(arr[0]);
cout << " Before Convert \n ";
cout << " After Convert \n ";
return 0;
}``````

Output

`````` Before Convert
3   5   8   2   10   6   9   5   3
After Convert
1   3   6   0   8   5   7   4   2``````
``````// Include namespace system
using System;
using System.Linq;
using System.Collections.Generic;
/*
C# Program
Convert an array to reduced form
*/
public class Reduction
{
//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");
}
// Handles the request to get sorted order element position
public void convert(int[] arr, int n)
{
if (n <= 0)
{
return;
}
// Used to collecting the sorted order sequence
Dictionary <int, HashSet <int> > record
= new Dictionary < int, HashSet < int > > ();
// Loop controlling variable
int temp = 0;
int i = 0;
// Auxiliary array
int[] auxiliary = (int[])arr.Clone();
// Sort array elements in ascending order
Array.Sort(auxiliary);
// Get order of sorted elements in proper way
for (i = 0; i < n; ++i)
{
if (record.ContainsKey(auxiliary[i]))
{
// When element is repeated
}
else
{
record.Add(auxiliary[i], new HashSet < int > ());
}
}
// Assign the calculated result to actual array
for (i = 0; i < n; ++i)
{
// Get sorted position
temp = record[arr[i]].First();
// Remove first element
record[arr[i]].Remove(temp);
// Assign sort position
arr[i] = temp;
}
}
public static void Main(String[] arg)
{
// Define array of integer elements
// Element duplicates are allowed
int[] arr = {
3 , 5 , 8 , 2 , 10 , 6 , 9 , 5 , 3
};
// Get the length of array
int n = arr.Length;
Console.Write(" Before Convert \n ");
Console.Write(" After Convert \n ");
}
}``````

Output

`````` Before Convert
3   5   8   2   10   6   9   5   3
After Convert
1   3   6   0   8   5   7   4   2``````
``````<?php
/*
Php Program
Convert an array to reduced form
*/
class Reduction
{
//Function which is display array elements
public
function display( & \$arr, \$n)
{
for (\$i = 0; \$i < \$n; ++\$i)
{
echo "   ".\$arr[\$i];
}
echo "\n";
}
// Handles the request to get sorted order element position
public
function convert( & \$arr, \$n)
{
if (\$n <= 0)
{
return;
}
// Used to collecting the sorted order sequence
\$record = array();
// Loop controlling variable
\$temp = 0;
\$i = 0;
// Auxiliary array
\$auxiliary = array_merge(array(), \$arr);
sort(\$auxiliary);
// Get order of sorted elements in proper way
for (\$i = 0; \$i < \$n; ++\$i)
{
if (array_key_exists(\$auxiliary[\$i], \$record))
{
\$record[\$auxiliary[\$i]][] = \$i;
}
else
{
\$record[\$auxiliary[\$i]] = array();
\$record[\$auxiliary[\$i]][] = \$i;
}
}
// Assign the calculated result to actual array
for (\$i = 0; \$i < \$n; ++\$i)
{
// Get sorted position
\$temp = reset(\$record[\$arr[\$i]]);
array_shift(\$record[\$arr[\$i]]);
// Assign sort position
\$arr[\$i] = \$temp;
}
}
}

function main()
{
// Define array of integer elements
// Element duplicates are allowed
\$arr = array(3, 5, 8, 2, 10, 6, 9, 5, 3);
// Get the length of array
\$n = count(\$arr);
echo " Before Convert \n ";
echo " After Convert \n ";
}
main();``````

Output

`````` Before Convert
3   5   8   2   10   6   9   5   3
After Convert
1   3   6   0   8   5   7   4   2``````
``````/*
Node Js Program
Convert an array to reduced form
*/
class Reduction
{
//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");
}
// Handles the request to get sorted order element position
convert(arr, n)
{
if (n <= 0)
{
return;
}
// Used to collecting the sorted order sequence

var record = new Map();
// Loop controlling variable
var temp = 0;
var i = 0;
// Auxiliary array
var auxiliary = arr.slice();
// Sort array elements in ascending order
auxiliary.sort(function(x, y){return x-y});
// Get order of sorted elements in proper way
for (i = 0; i < n; ++i)
{
if (record.has(auxiliary[i]))
{
// When element is repeated
}
else
{
record.set(auxiliary[i], new Set());
}
}
// Assign the calculated result to actual array
for (i = 0; i < n; ++i)
{
// Get sorted position
temp = record.get(arr[i]).values().next().value;

// Remove first element
record.get(arr[i]).delete(temp);
// Assign sort position
arr[i] = temp;
}
}
}

function main()
{
// Define array of integer elements
// Element duplicates are allowed
var arr = [3, 5, 8, 2, 10, 6, 9, 5, 3];
// Get the length of array
var n = arr.length;
process.stdout.write(" Before Convert \n ");
process.stdout.write(" After Convert \n ");
}
main();``````

Output

`````` Before Convert
3   5   8   2   10   6   9   5   3
After Convert
1   3   6   0   8   5   7   4   2``````
``````#    Python 3 Program
#    Convert an array to reduced form

class Reduction :
# 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")

#  Handles the request to get sorted order element position
def convert(self, arr, n) :
if (n <= 0) :
return

#  Used to collecting the sorted order sequence
record = dict()
#  Loop controlling variable
temp = 0
i = 0
#  Auxiliary list
auxiliary = arr.copy()
#  Sort list elements in ascending order
auxiliary.sort()
#  Get order of sorted elements in proper way
i = 0
while (i < n) :
if (auxiliary[i] in record.keys()) :
#  When element is repeated
else :
record[auxiliary[i]] = set()

i += 1

#  Assign the calculated result to actual list
i = 0
while (i < n) :
#  Get sorted position
temp = record.get(arr[i]).pop()
#  Assign sort position
arr[i] = temp
i += 1

def main() :
#  Define list of integer elements
#  Element duplicates are allowed
arr = [3, 5, 8, 2, 10, 6, 9, 5, 3]
#  Get the length of list
n = len(arr)
print(" Before Convert \n ", end = "")
print(" After Convert \n ", end = "")

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

Output

`````` Before Convert
3   5   8   2   10   6   9   5   3
After Convert
1   3   6   0   8   5   7   4   2``````
``````#    Ruby Program
#    Convert an array to reduced form

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

print("\n")
end

#  Handles the request to get sorted order element position
def convert(arr, n)
if (n <= 0)
return
end

#  Used to collecting the sorted order sequence
record = Hash.new
#  Loop controlling variable
temp = 0
i = 0
#  Auxiliary array
auxiliary = arr.sort

#  Get order of sorted elements in proper way
i = 0
while (i < n)
if (record.key?(auxiliary[i]))
record[auxiliary[i]].push(i)
else
record[auxiliary[i]] = []
record[auxiliary[i]].push(i)
end

i += 1
end

#  Assign the calculated result to actual array
i = 0
while (i < n)
#  Get sorted position
temp = record[arr[i]].pop()
#  Assign sort position
arr[i] = temp
i += 1
end

end

end

def main()
#  Define array of integer elements
#  Element duplicates are allowed
arr = [3, 5, 8, 2, 10, 6, 9, 5, 3]
#  Get the length of array
n = arr.length
print(" Before Convert \n ")
print(" After Convert \n ")
end

main()``````

Output

`````` Before Convert
3   5   8   2   10   6   9   5   3
After Convert
2   4   6   0   8   5   7   3   1
``````
``````import scala.collection.mutable._;
/*
Scala Program
Convert an array to reduced form
*/
class Reduction
{
//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");
}
// Handles the request to get sorted order element position
def convert(arr: Array[Int], n: Int): Unit = {
if (n <= 0)
{
return;
}
// Used to collecting the sorted order sequence
var record: Map[Int, Set[Int]] = Map();
// Loop controlling variable
var temp: Int = 0;
var i: Int = 0;
// Auxiliary array
var auxiliary: Array[Int] = Array.fill[Int](n)(0);
// Sort array elements in ascending order
while (i < n)
{
auxiliary(i) = arr(i);
i += 1;
}
scala.util.Sorting.quickSort(auxiliary);
i = 0;
while (i < n)
{
if (record.contains(auxiliary(i)))
{
// When element is repeated
}
else
{
}
i += 1;
}
// Assign the calculated result to actual array
i = 0;
while (i < n)
{
// Get sorted position
// Remove first element
record.get(arr(i)).get.remove(temp);
// Assign sort position
arr(i) = temp;
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Reduction = new Reduction();
// Define array of integer elements
// Element duplicates are allowed
var arr: Array[Int] = Array(3, 5, 8, 2, 10, 6, 9, 5, 3);
// Get the length of array
var n: Int = arr.length;
print(" Before Convert \n ");
print(" After Convert \n ");
}
}``````

Output

`````` Before Convert
3   5   8   2   10   6   9   5   3
After Convert
1   3   6   0   8   5   7   4   2``````
``````import Foundation
/*
Swift 4 Program
Convert an array to reduced form
*/
class Reduction
{
//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");
}
// Handles the request to get sorted order element position
func convert(_ arr: inout[Int], _ n: Int)
{
if (n <= 0)
{
return;
}
// Used to collecting the sorted order sequence
var record = [Int: Set <Int> ]();
// Loop controlling variable
var temp: Int = 0;
var i: Int = 0;
// Auxiliary array
var auxiliary: [Int] = Array(repeating: 0, count: n);
// Sort array elements in ascending order
while (i < n)
{
auxiliary[i] = arr[i];
i += 1;
}
auxiliary.sort();
i = 0;
while (i < n)
{
if (record.keys.contains(auxiliary[i]))
{
// When element is repeated
record[auxiliary[i]]!.insert(i);
}
else
{
record[auxiliary[i]] = Set<Int>();
record[auxiliary[i]]!.insert(i);
}
i += 1;
}
// Assign the calculated result to actual array
i = 0;
while (i < n)
{
// Get sorted position
temp = record[arr[i]]!.first!;
// Remove first element
record[arr[i]]!.remove(temp);
// Assign sort position
arr[i] = temp;
i += 1;
}
}
}
func main()
{
// Define array of integer elements
// Element duplicates are allowed
var arr: [Int] = [3, 5, 8, 2, 10, 6, 9, 5, 3];
// Get the length of array
let n: Int = arr.count;
print(" Before Convert \n ", terminator: "");
print(" After Convert \n ", terminator: "");
}
main();``````

Output

`````` Before Convert
3    5    8    2    10    6    9    5    3
After Convert
2    3    6    0    8    5    7    4    1``````
``````/*
Kotlin Program
Convert an array to reduced form
*/
class Reduction
{
//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");
}
// Handles the request to get sorted order element position
fun convert(arr: Array < Int > , n: Int): Unit
{
if (n <= 0)
{
return;
}
// Used to collecting the sorted order sequence
var record = mutableMapOf<Int, MutableSet <Int>>();
// Loop controlling variable
var temp: Int ;
var i: Int = 0;
// Auxiliary array
var auxiliary: Array < Int > = Array(n)
{
0
};
// Sort array elements in ascending order
while (i < n)
{
auxiliary[i] = arr[i];
i += 1;
}
auxiliary.sort();
i = 0;
while (i < n)
{
if (record.containsKey(auxiliary[i]))
{
// When element is repeated
}
else
{
record.put(auxiliary[i], mutableSetOf <Int> ());
}
i += 1;
}
// Assign the calculated result to actual array
i = 0;
while (i < n)
{
// Get sorted position
temp = record.getValue(arr[i]).elementAt(0);
// Remove first element
record.getValue(arr[i]).remove(temp);
// Assign sort position
arr[i] = temp;
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
// Define array of integer elements
// Element duplicates are allowed
var arr: Array < Int > = arrayOf(3, 5, 8, 2, 10, 6, 9, 5, 3);
// Get the length of array
var n: Int = arr.count();
print(" Before Convert \n ");
print(" After Convert \n ");
}``````

Output

`````` Before Convert
3   5   8   2   10   6   9   5   3
After Convert
1   3   6   0   8   5   7   4   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.