1

Let me explain, let's say I have this array:

var dateDouble_ARRAY = [Date : [Double] ]()

dateDouble_ARRAY = [2017-08-06 07:00:00 +0000: [11.880000000000001, 3.0], 
    2017-08-08 07:00:00 +0000: [5.0], 
    2017-08-01 07:00:00 +0000: [6.0], 
    2017-08-09 07:00:00 +0000: [6.0], 
    2017-08-02 07:00:00 +0000: [5.0], 
    2017-08-03 07:00:00 +0000: [6.5499999999999998], 
    2017-08-10 07:00:00 +0000: [2.0] ]

Each Date key has a value that could consist of multiple Double numbers.

What I would like to find out is how can I find the largest value in this array?

For example, in this case, the largest value is 11.880000000000001 + 3.0 = 14.88 (rounded) because the key-value pair consists of 2 Double values, and 14.88 is larger then any other key-value pair.

However, if the array consisted of:

dateDouble_ARRAY = [2017-08-06 07:00:00 +0000: [3.0], 
    2017-08-08 07:00:00 +0000: [5.0], 
    2017-08-01 07:00:00 +0000: [6.0], 
    2017-08-09 07:00:00 +0000: [6.0], 
    2017-08-02 07:00:00 +0000: [5.0], 
    2017-08-03 07:00:00 +0000: [6.5499999999999998], 
    2017-08-10 07:00:00 +0000: [2.0] ]

Then the largest value would be 6.55 (rounded).

How can I achieve this?

Pangu
  • 3,721
  • 11
  • 53
  • 120

2 Answers2

2

Try with this

let maxValue = dateDouble_ARRAY.values.map({$0.reduce(0,+)}).max()

if you want to round then

let maxValue = dateDouble_ARRAY.values.map({yourRoundMethod($0.reduce(0,+))}).max()

tested

Input

[2016-02-02 08:00:00 +0000: [2], 2016-05-01 08:00:00 +0000: [5, 6], 2016-03-02 08:00:00 +0000: [3, 4], 2016-01-02 08:00:00 +0000: [0, 1]]

Result

11

Hope this helps

Reinier Melian
  • 20,519
  • 3
  • 38
  • 55
0

reduce and then reduce some more

 let value = dateDouble_ARRAY.values.reduce(0){max($0, $1.reduce(0,+))}
 print(value)
Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33