Array range queries for elements with frequency same as value | Returns the count of number x with frequency x in the subarray from start to end ; map for frequency of elements ; store frequency of each element in arr [ start ; end ] ; Count elements with same frequency as value ; Driver code ; 2D array of queries with 2 columns ; calculating number of queries
function solveQuery ( start , end , arr ) { let mp = new Map ( ) ; for ( let i = start ; i <= end ; i ++ ) mp . set ( arr [ i ] , mp . get ( arr [ i ] ) == null ? 1 : mp . get ( arr [ i ] ) + 1 ) ; let count = 0 ; for ( let [ key , value ] of mp . entries ( ) ) if ( key == value ) count ++ ; return count ; } let A = [ 1 , 2 , 2 , 3 , 3 , 3 ] ; let n = A . length ; let queries = [ [ 0 , 1 ] , [ 1 , 1 ] , [ 0 , 2 ] , [ 1 , 3 ] , [ 3 , 5 ] , [ 0 , 5 ] ] ; let q = queries . length ; for ( let i = 0 ; i < q ; i ++ ) { let start = queries [ i ] [ 0 ] ; let end = queries [ i ] [ 1 ] ; document . write ( " " + ( i + 1 ) + " " + solveQuery ( start , end , A ) + " " ) ; }