Created
March 8, 2016 23:17
-
-
Save JRuumis/8a7d47c0485fcbb44c37 to your computer and use it in GitHub Desktop.
success at last!
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
| 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