Deden Bangkit - 1 year ago 75
Scala Question

# Remove specific element on list with math logic in scala

If I have this list:

``````val aList =  List(1,1,1,3,4),List(3,3,5,6,7),List(7,7,7,6,7),List(2,3,3,2,6)
``````

How do I update the list by eliminating non-duplicated numbers on the first head of the
`List`
? so the result should be:

`val aRes = List(1,1,1), List(3,3), List(7,7,7)`

`List(2,3,3,2,6)`
should be removed also since we don't have 3 at the head of the list. My expectation for the result was:

`val aRes = aList(1) map {case List(i) => List(aList.groupBy(_(1))}`

But it seems not valid for this logic.

beside that, I also need to translate those result values to another list member:

``````val aScore = List(
/*score for 1*/ List(0, 0, 1500, 2500, 5000),
/*score for 2*/ List(0, 0, 500, 1000, 2000),
/*score for 3*/ List(0, 50, 100, 200, 500),
/*score for 4*/ List(0, 10, 50, 100, 150),
/*score for 5*/ List(0, 10, 50, 100, 150),
/*score for 6*/ List(0, 10, 50, 100, 150),
/*score for 7*/ List(0, 10, 50, 100, 150)
)
``````

`val score = ???`

so from above
`aList`
result, the score must be
`1500+50+50 = 1600`
as result from
`1*3=>1500`
,
`3*2=>50`
and
`7*3=>50`

``````object parseData {
val inputList = List(List(1,1,1,3,4),List(3,3,5,6,7),List(7,7,7,6,7),List(2,3,3,2,6))
val aScore = List(
/*score for 1*/ List(0, 0, 1500, 2500, 5000),
/*score for 2*/ List(0, 0, 500, 1000, 2000),
/*score for 3*/ List(0, 50, 100, 200, 500),
/*score for 4*/ List(0, 10, 50, 100, 150),
/*score for 5*/ List(0, 10, 50, 100, 150),
/*score for 6*/ List(0, 10, 50, 100, 150),
/*score for 7*/ List(0, 10, 50, 100, 150)
)

def getRidOfNonDuplicates(aList: List[Int]): List[Int] = {
def loop(aList: List[Int], opList: ListBuffer[Int]): Unit = {
if (aList.tail == Nil) return
loop(aList.tail, opList)
}
loop(aList, opList)
opList.toList
}

def printaScoreValue(aList: List[Int]): Unit = println(aScore(aList.head - 1)(aList.length - 1))

val outputList = inputList.filter(isDuplicated(_))
val opList = ListBuffer.empty[List[Int]]
for (aList <- outputList)
opList += getRidOfNonDuplicates(aList)
opList.foreach(printaScoreValue(_))
}
``````

gives

``````1500
50
50
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download