Search code examples
javaarraylisthashmap

Can this be done without a HashMap?


I've created a simple game where 2 players enter inputs and the one with the bigger lead is declared the winner at the end of the game along with the winning margin. The first input you enter is the number of turns each player gets. In the output, I needed the player number and the margin mentioned. I was able to get it done but I want to know if this can be done without using a HashMap as I'm already using an arraylist as well. Can someone suggest a way for doing this?

    Scanner o = new Scanner(System.in);
    List<Integer> arrlist = new ArrayList<Integer>();
    int q = o.nextInt(), a1, b1, diff, max, w;
    HashMap<Integer, Integer> map = new HashMap<>();

    for (int i = 0; i < q; i++) {   // q is the number of turns
        a1 = o.nextInt();   //first player input
        b1 = o.nextInt();   //second player input
        diff = (a1 > b1) ? a1 - b1 : b1 - a1;   //getting the difference
        w = (a1 > b1) ? 1 : 2;   //picking the winner for the turn
        map.put(diff, w);
        arrlist.add(diff);
    }

    max = arrlist.get(0); 
    for (int j = 0; j < arrlist.size(); j++) { 
        if (arrlist.get(j) > max)
            max = arrlist.get(j); 
    } 
    System.out.printf("%d %d", map.get(max), max );  

Solution

  • Edited: Only 1 class solution:

    public static void main(String[] args) {
        Scanner o = new Scanner(System.in);
        System.out.print("Input max match: ");
        int q = o.nextInt();
        int maxDiff = 0;
        int maxWinner = 0;
    
        for (int i = 0; i < q; i++) {   // q is the number of turns
            System.out.println("Match " + String.valueOf(i + 1));
            System.out.print("1st player: ");
            int player1Input = o.nextInt();   //first player input
            System.out.print("2st player: ");
            int player2Input = o.nextInt();   //second player input
            int diff = Math.abs(player1Input - player2Input);   //getting the difference
            int winner = (player1Input > player2Input) ? 1 : 2;   //picking the winner for the turn
    
            if(diff > maxDiff) {
                maxDiff = diff;
                maxWinner = winner;
            }
        }
    
        if(maxWinner > 0) {
            System.out.printf("Winner player: %d Max diff: %d", maxWinner, maxDiff);
        }
    }  
    

    Old solution: Add Match class to store diff + winner

    public class StackOverflow_59349187 {
    
        public static void main(String[] args) {
            Scanner o = new Scanner(System.in);
            System.out.print("Input max match: ");
            int q = o.nextInt();
    
            List<Match> matches = new ArrayList<>();
    
            for (int i = 0; i < q; i++) {   // q is the number of turns
                System.out.println("Match " + String.valueOf(i + 1));
                System.out.print("1st player: ");
                int player1Input = o.nextInt();   //first player input
                System.out.print("2st player: ");
                int player2Input = o.nextInt();   //second player input
                int diff = Math.abs(player1Input - player2Input);   //getting the difference
                int winner = (player1Input > player2Input) ? 1 : 2;   //picking the winner for the turn
                Match match = new Match(winner, diff);
                matches.add(match);
            }
    
            if(matches.size() > 0) {
                Match maxDiffMatch = matches.get(0);
                for (int j = 0; j < matches.size(); j++) {
                    Match currentMatch = matches.get(j);
                    int currentDiff = currentMatch.getDiff();
                    int maxDiff = maxDiffMatch.getDiff();
                    if (currentDiff > maxDiff) {
                        maxDiffMatch = currentMatch;
                    }
                }
                System.out.printf("Winner player: %d Max diff: %d", maxDiffMatch.getPlayer(), maxDiffMatch.getDiff());
            }
        }
    
        static class Match {
            private final int player;
            private final int diff;
    
            public Match(int player, int diff) {
                this.player = player;
                this.diff = diff;
            }
    
    
            public int getPlayer() {
                return player;
            }
    
            public int getDiff() {
                return diff;
            }
        }
    }