Convert an array to reduced form | Set 1 ( Simple and Hashing ) | Javascript 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 program
function convert ( arr , n ) { let temp = [ ... arr ] ; temp . sort ( ( a , b ) => a - b ) ; let umap = new Map ( ) ; let val = 0 ; for ( let i = 0 ; i < n ; i ++ ) umap . set ( temp [ i ] , val ++ ) ; for ( let i = 0 ; i < n ; i ++ ) arr [ i ] = umap . get ( arr [ i ] ) ; } function prletArr ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 10 , 20 , 15 , 12 , 11 , 50 ] ; let n = arr . length ; document . write ( " " + " " ) ; prletArr ( arr , n ) ; convert ( arr , n ) ; document . write ( " " + " " + " " ) ; prletArr ( arr , n ) ;