Two player game in which a player can remove all occurrences of a number | Java implementation for Two player game in which a player can remove all occurrences of a number ; Function that print whether player1 can wins or loses ; Storing the number of occurrence of elements in unordered map ; variable to check if the occurrence of repeated elements is >= 4 and multiple of 2 or not ; count elements which occur more than once ; Driver code
import java . util . * ; class GFG { public static void game ( int [ ] v , int n ) { HashMap < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! m . containsKey ( v [ i ] ) ) m . put ( v [ i ] , 1 ) ; else m . replace ( v [ i ] , m . get ( v [ i ] ) + 1 ) ; } int count = 0 ; int check = 0 ; for ( Map . Entry < Integer , Integer > i : m . entrySet ( ) ) { if ( i . getValue ( ) > 1 ) { if ( i . getValue ( ) >= 4 && i . getValue ( ) % 2 == 0 ) check ++ ; count ++ ; } } boolean flag ; if ( check % 2 != 0 ) flag = false ; if ( check % 2 != 0 ) System . out . println ( " Yes " ) ; else if ( n % 2 == 0 && count % 2 == 0 ) System . out . println ( " No " ) ; else System . out . println ( " Yes " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 2 , 3 , 3 , 5 } ; int size = arr . length ; game ( arr , size ) ; } }