Advertisement
saurav_kalsoor

Votes Count - KOTLIN

Jul 25th, 2021 (edited)
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.73 KB | None | 0 0
  1. package com.company
  2.  
  3. import java.util.*
  4.  
  5. fun main() {
  6.     val sc = Scanner(System.`in`)
  7.     val n = sc.nextInt()
  8.     val votes = ArrayList<String>()
  9.     for (i in 0 until n) {
  10.         val vote = sc.next()
  11.         votes.add(vote)
  12.     }
  13.     val winner = votesCount(votes, n)
  14.     println(winner)
  15. }
  16.  
  17. fun votesCount(votes: ArrayList<String>, n: Int): String {
  18.     var res = 0
  19.     var count = 1
  20.     for (i in 1 until n) {
  21.         if (votes[i] == votes[res]) count++ else {
  22.             count--
  23.             if (count == 0) {
  24.                 count = 1
  25.                 res = i
  26.             }
  27.         }
  28.     }
  29.     count = 0
  30.     for (vote in votes) if (vote == votes[res]) count++
  31.     return if (count > n / 2) votes[res] else "DRAW"
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement