Skip to content

Instantly share code, notes, and snippets.

@vamsitallapudi
Created November 7, 2018 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vamsitallapudi/d0645e7018b8ec0b720fe34cb1d38bcb to your computer and use it in GitHub Desktop.
Save vamsitallapudi/d0645e7018b8ec0b720fe34cb1d38bcb to your computer and use it in GitHub Desktop.
package main.interview.hacklandelection
import com.sun.xml.internal.fastinfoset.util.StringArray
import java.util.*
fun main() {
val list = StringArray()
var iter = readLine()!!.toInt()
while(iter-- > 0) {
list.add(readLine()!!)
}
electionWinner(list)
}
fun electionWinner(list: StringArray) {
var winner = ""
var max = 0
val hashMap = HashMap<String, Int>()
for (i in 0 until list.size) {
if (hashMap.containsKey(list[i])) {
hashMap[list[i]] = hashMap.getValue(list[i])+1
} else {
hashMap[list[i]] = 0
}
}
for (entry in hashMap.entries) {
if(entry.value > max) {
max = entry.value
winner = entry.key
} else if(entry.value == max) {
winner = if(entry.key > winner) entry.key else winner
}
}
println(winner)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment