Check if all array elements are present in a given stack or not | Function to check if all array elements is present in the stack ; Store the frequency of array elements ; Loop while the elements in the stack is not empty ; Condition to check if the element is present in the stack ; Driver code
function checkArrInStack ( s , arr , n ) { var freq = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( freq . has ( arr [ i ] ) ) freq . set ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) else freq . set ( arr [ i ] , 1 ) } while ( s . length != 0 ) { var poppedEle = s [ s . length - 1 ] ; s . pop ( ) ; if ( freq . has ( poppedEle ) ) freq . set ( poppedEle , freq . get ( poppedEle ) - 1 ) ; } if ( freq . size == 0 ) return 0 ; return 1 ; } var s = [ ] ; s . push ( 10 ) ; s . push ( 20 ) ; s . push ( 30 ) ; s . push ( 40 ) ; s . push ( 50 ) ; var arr = [ 20 , 30 ] ; var n = arr . length ; if ( checkArrInStack ( s , arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;