Check if all array elements are present in a given stack or not | C # program of the above approach ; 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
using System ; using System . Collections . Generic ; class GFG { static bool checkArrInStack ( Stack < int > s , int [ ] arr , int n ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( freq . ContainsKey ( arr [ i ] ) ) freq [ arr [ i ] ] = freq [ arr [ i ] ] + 1 ; else freq . Add ( arr [ i ] , 1 ) ; while ( s . Count != 0 ) { int poppedEle = s . Peek ( ) ; s . Pop ( ) ; if ( freq . ContainsKey ( poppedEle ) ) freq [ poppedEle ] = freq [ poppedEle ] - 1 ; } if ( freq . Count == 0 ) return false ; return true ; } public static void Main ( String [ ] args ) { Stack < int > s = new Stack < int > ( ) ; s . Push ( 10 ) ; s . Push ( 20 ) ; s . Push ( 30 ) ; s . Push ( 40 ) ; s . Push ( 50 ) ; int [ ] arr = { 20 , 30 } ; int n = arr . Length ; if ( checkArrInStack ( s , arr , n ) ) Console . Write ( " YES STRNEWLINE " ) ; else Console . Write ( " NO STRNEWLINE " ) ; } }