Replace each element of Array with it 's corresponding rank | C # program for the above approach ; Function to assign rank to array elements ; Copy input array into newArray ; Sort newArray [ ] in ascending order ; To store the rank of the array element ; Update rank of element ; Assign ranks to elements ; Driver Code ; Given array arr [ ] ; Function call ; Print the array elements
using System ; using System . Collections . Generic ; using System . Text ; class GFG { static void changeArr ( int [ ] input ) { int [ ] newArray = new int [ input . Length ] ; Array . Copy ( input , newArray , input . Length ) ; Array . Sort ( newArray ) ; Dictionary < int , int > ranks = new Dictionary < int , int > ( ) ; int rank = 1 ; for ( int index = 0 ; index < newArray . Length ; index ++ ) { int element = newArray [ index ] ; if ( ! ranks . ContainsKey ( element ) ) { ranks [ element ] = rank ; rank ++ ; } } for ( int index = 0 ; index < input . Length ; index ++ ) { input [ index ] = ranks [ input [ index ] ] ; } } public static void Main ( string [ ] args ) { int [ ] arr = { 100 , 2 , 70 , 2 } ; changeArr ( arr ) ; Console . WriteLine ( " [ { 0 } ] " , string . Join ( " , ▁ " , arr ) ) ; } }