Question:
I have array contain string items in scala , each item contain from prefix + || + double value like below :i need sum all double value from above array (y(i).split(“\|\|”)(1)) But if the prefix the duplicated in the array then I only want sum the max value like below : for item Zara we have 3 values i want to take the max (in our sample it 6.0) for item Nuha it unique then i will take it’s value (4.0)
the excepted output is (6.0+4.0)=10.0
is there are any way to do it in scala rather than using 2 instead loop ?
Answer:
Prepare your array: extract prefix and values into tuple. Use foldLeft for aggregate max elem for each prefix, and sum valuesIf you have better answer, please add a comment about this, thank you!