• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: how to sum only the max value for common prefix inside the array in scala

Resolved: how to sum only the max value for common prefix inside the array in scala

0
By Isaac Tonny on 18/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I have array contain string items in scala , each item contain from prefix + || + double value like below :
what I want to Do :
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 values

If you have better answer, please add a comment about this, thank you!

arrays scala
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Input Focus between two React Components

02/04/2023

Resolved: linq2db throws exception when filtering by nested collection

02/04/2023

Resolved: Table data is coming as empty in React

02/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.