Minimize swaps required to maximize the count of elements replacing a greater element in an Array | C # program to implement the above approach ; Function to find the minimum number of swaps required ; Stores the frequency of the array elements ; Stores maximum frequency ; Find the max frequency ; Update frequency ; Update maximum frequency ; Driver Code ; Function call
using System ; using System . Collections . Generic ; class GFG { static int countSwaps ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int max_frequency = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } max_frequency = Math . Max ( max_frequency , mp [ arr [ i ] ] ) ; } return n - max_frequency ; } public static void Main ( String [ ] args ) { int [ ] A = { 6 , 5 , 4 , 3 , 2 , 1 } ; Console . Write ( countSwaps ( A , 6 ) ) ; } }