Skip to content

Instantly share code, notes, and snippets.

@JRuumis
Created March 8, 2016 23:17
Show Gist options
  • Select an option

  • Save JRuumis/8a7d47c0485fcbb44c37 to your computer and use it in GitHub Desktop.

Select an option

Save JRuumis/8a7d47c0485fcbb44c37 to your computer and use it in GitHub Desktop.
success at last!
object FilterElements4 /* rename this to Solution for Hackerrank */ extends App {
// helper functions
def occurringElements(occur: Int, list: List[Int]):Set[Int] = {
list.groupBy(identity).mapValues( _.size ).filter{ case(key,value) => value >= occur }.keys.toSet
}
def occurringListElements (list: List[Int], occurringElements: Set[Int]): List[Int] = {
if (list == Nil || occurringElements.isEmpty) Nil
else {
val (l :: rest) = list
if ( occurringElements contains l ) l :: occurringListElements( rest, occurringElements - l )
else occurringListElements( rest, occurringElements )
}
}
// MAIN
val testCases = io.StdIn.readInt()
Range(0,testCases)
.map { _ => ( io.StdIn.readLine().split(' ')(1).toInt , io.StdIn.readLine().split(' ').toList.map(_.toInt) ) }
.map { case (occurrences, list) => ( list, occurringElements(occurrences, list) ) }
.map { case (list, occurringElements) => occurringListElements(list, occurringElements)}
.map { l => if (l == Nil) List(-1) else l }
.map { l => println( l.mkString(" ") ) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment