Minimum , maximum and average price values for all the items of given type | C # implementation of the approach ; To store an item ; To store the minimum and the maximum price for the item ; To store the total number of items of the current type and the total cost of buying them ; Initializing an element ; Function to find the minimum , the maximum and the average price for every item ; To store the distinct items ; For every item ; If the current item has aready been purchased earlier from a different shop ; Get the item ; Update its minimum and maximum price so far ; Increment the total count of the current item ; Add the current price to the sum ; The item has been purchased for the first time ; Print all the items with their minimum , maximum and average prices ; Driver code
using System ; using System . Collections . Generic ; class GFG { public class Item { public int min , max ; public int total , sum ; public Item ( int price ) { min = price ; max = price ; total = 1 ; this . sum = price ; } } static void findPrices ( String [ ] item , int [ ] price , int n ) { Dictionary < String , Item > map = new Dictionary < String , Item > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( map . ContainsKey ( item [ i ] ) ) { Item currItem = map [ item [ i ] ] ; currItem . min = Math . Min ( currItem . min , price [ i ] ) ; currItem . max = Math . Max ( currItem . max , price [ i ] ) ; currItem . total ++ ; currItem . sum += price [ i ] ; } else { Item currItem = new Item ( price [ i ] ) ; map . Add ( item [ i ] , currItem ) ; } } Console . WriteLine ( " Item ▁ Min ▁ Max ▁ Average " ) ; foreach ( KeyValuePair < String , Item > ob in map ) { String key = ob . Key ; Item currItem = ob . Value ; Console . WriteLine ( key + " ▁ " + currItem . min + " ▁ " + currItem . max + " ▁ " + ( ( float ) currItem . sum / ( float ) currItem . total ) ) ; } } public static void Main ( String [ ] args ) { String [ ] item = { " toy " , " pen " , " notebook " , " pen " } ; int n = item . Length ; int [ ] price = { 2 , 1 , 3 , 2 } ; findPrices ( item , price , n ) ; } }