Convert an array to reduced form | Set 1 ( Simple and Hashing ) | C # Program to convert an Array to reduced form ; Create a temp array and copy contents of [ ] arr to temp ; Sort temp array ; Create a hash table . ; One by one insert elements of sorted [ ] temp and assign them values from 0 to n - 1 ; Convert array by taking positions from umap ; Driver code
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { public static void convert ( int [ ] arr , int n ) { int [ ] temp = new int [ arr . Length ] ; Array . Copy ( arr , 0 , temp , 0 , arr . Length ) ; Array . Sort ( temp ) ; Dictionary < int , int > umap = new Dictionary < int , int > ( ) ; int val = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( umap . ContainsKey ( temp [ i ] ) ) umap [ temp [ i ] ] = val ++ ; else umap . Add ( temp [ i ] , val ++ ) ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = umap [ arr [ i ] ] ; } public static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 20 , 15 , 12 , 11 , 50 } ; int n = arr . Length ; Console . WriteLine ( " Given ▁ Array ▁ is ▁ " ) ; printArr ( arr , n ) ; convert ( arr , n ) ; Console . WriteLine ( " STRNEWLINE STRNEWLINE Converted ▁ Array ▁ is ▁ " ) ; printArr ( arr , n ) ; } }