Created
November 7, 2018 05:07
-
-
Save vamsitallapudi/d0645e7018b8ec0b720fe34cb1d38bcb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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