Count items common to both the lists but with different prices | C # implementation to count items common to both the lists but with different prices ; Details of an item ; Function to count items common to both the lists but with different prices ; ' um ' implemented as hash table that contains item name as the key and price as the value associated with the key ; Insert elements of ' list1' in ' um ' ; For each element of ' list2' check if it is present in ' um ' with a different price value ; Required count of items ; Driver code
using System ; using System . Collections . Generic ; class GFG { public class item { public String name ; public int price ; public item ( String name , int price ) { this . name = name ; this . price = price ; } } ; static int countItems ( item [ ] list1 , int m , item [ ] list2 , int n ) { Dictionary < String , int > um = new Dictionary < String , int > ( ) ; int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) um . Add ( list1 [ i ] . name , list1 [ i ] . price ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ( um . ContainsKey ( list2 [ i ] . name ) ) && ( um [ list2 [ i ] . name ] != list2 [ i ] . price ) ) count ++ ; return count ; } public static void Main ( String [ ] args ) { item [ ] list1 = { new item ( " apple " , 60 ) , new item ( " bread " , 20 ) , new item ( " wheat " , 50 ) , new item ( " oil " , 30 ) } ; item [ ] list2 = { new item ( " milk " , 20 ) , new item ( " bread " , 15 ) , new item ( " wheat " , 40 ) , new item ( " apple " , 60 ) } ; int m = list1 . Length ; int n = list2 . Length ; Console . Write ( " Count ▁ = ▁ " + countItems ( list1 , m , list2 , n ) ) ; } }