Advertisement
saurav_kalsoor

Votes Count - JAVA

Jul 25th, 2021 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner sc = new Scanner(System.in);
  10.         int n = sc.nextInt();
  11.         ArrayList<String> votes = new ArrayList<>();
  12.         for(int i=0;i<n;i++){
  13.             String vote = sc.next();
  14.             votes.add(vote);
  15.         }
  16.         String winner = votesCount(votes, n);
  17.         System.out.println(winner);
  18.     }
  19.  
  20.     public static String votesCount(ArrayList<String> votes, int n){
  21.         int res = 0;
  22.         int count = 1;
  23.         for(int i=1 ; i < n; i++){
  24.             if(votes.get(i).equals(votes.get(res)))
  25.                 count++;
  26.             else{
  27.                 count--;
  28.                 if(count == 0){
  29.                     count = 1;
  30.                     res = i;
  31.                 }
  32.             }
  33.         }
  34.         count = 0;
  35.         for(String vote : votes)
  36.             if(vote.equals(votes.get(res)))
  37.                 count++;
  38.  
  39.         if(count > n/2)
  40.             return votes.get(res);
  41.  
  42.         return "DRAW";
  43.     }
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement