Skip to content

Instantly share code, notes, and snippets.

@niroj-office
Created May 26, 2020 02:08
Show Gist options
  • Save niroj-office/54748b99d847d004059a00e483a81b65 to your computer and use it in GitHub Desktop.
Save niroj-office/54748b99d847d004059a00e483a81b65 to your computer and use it in GitHub Desktop.
Program to find longest sequence in Scala. Example: input- List(0,1,5,6,2), Output - 0,1,2
val longestSequence = (numList: List[Int]) => {
val list = numList
.map(x => {
var n = 0; var list = List[Int]()
while (numList.sorted.distinct.containsSlice(x to x + n)) {
list = (x to x + n).toList; n += 1;
}
list
})
.map(x => (x, x.length))
.sortBy(_._2)(Ordering.Int.reverse)(0)
._1
.sorted(Ordering.Int.reverse)
var n = 1
var finalList = list
while (numList.filter(_ == list.max - n).length > 1) {
finalList = list.max - n :: finalList; n += 1
}
finalList.map(_.toString).reduce(_ + "," + _)
}
//Author: Niroj Pattnaik
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment