0

I'm having this issue with arrays. Maybe I'm approaching this in a weird way, you guys let me know. I have two arrays:

String[] months = {"January","February","March","April","May",
                   "June","July","August","September",
                   "October","November","December"};
double[] rainfall ={4.22, 3.18, 3.03, 3.52, 4.54, 5.55, 
                    4.71, 4.35, 5.26, 5.46, 4.78, 4.09};

These numbers represent rainfall. I run the algorithm to find the Most and the Least from double[] rainfall, and it gives me the number 3.03, which is march. My question is, How can I correlate that subscript to the one from the array String[] months so that it shows me the name of the month as well? Should I make a stacked array instead?

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
Hugo Diaz
  • 35
  • 1
  • 1
  • 3

3 Answers3

1

I suggest to change your data structure to map:

Map<String, Double> rainfallMap = new TreeMap<>();
rainfallMap.put("January", 4.22);
rainfallMap.put("February", 3.18);
rainfallMap.put("March", 3.03);
rainfallMap.put("April", 3.52);
rainfallMap.put("May", 4.54);
rainfallMap.put("June", 5.55);
rainfallMap.put("July", 4.71);
rainfallMap.put("August", 4.35);
rainfallMap.put("September", 5.26);
rainfallMap.put("October", 5.46);
rainfallMap.put("November", 4.78);
rainfallMap.put("December", 4.09);

Now you can do this (Java 8):

Entry<String, Double> minEntry = rainfallMap.entrySet().stream()
    .min((e1, e2) -> Double.compare(e1.getValue(), e2.getValue()))
    .get();

System.out.println(String.format("Minimal rainfall was %.2f in %s",
    minEntry.getValue(), minEntry.getKey()));

EDIT:

@Tagir Valeev suggested better retrieval of minimal entry:

Entry<String, Double> minEntry = 
    Collections.min(rainfallMap.entrySet(), Map.Entry.comparingByValue());

I compared both approached with JMH and @Tagir Valeev's suggestion is definitely better:

 Benchmark                          Mode  Cnt    Score    Error  Units
 Rainfall.getMinRaifallCollections  avgt    5   73.654 ±  3.768  ns/op
 Rainfall.getMinRainfallStreams     avgt    5  126.808 ± 10.559  ns/op
luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • 1
    Good answer, though I'd suggest using LinkedHashMap or TreeMap so the iteration order is always the same. If two entries had the same value, a normal (undefined-order) HashMap could produce different answers when running this code multiple times. – dbort Sep 19 '15 at 22:21
  • You don't need the Stream API here. Using simple `Entry minEntry = Collections.min(rainfallMap.entrySet(), Map.Entry.comparingByValue())` would yield the same result in shorter and more efficient way. Also you should not add [tag:java-stream] tag to the question if the problem *can* be solved via stream api unless the OP explicitly asked for such solution. It can be solved using tons of other APIs/libraries (Apache commons, Guava, etc.) should we add all of them to the tags? – Tagir Valeev Sep 27 '15 at 03:58
0

Well, you'll need a linear search to get the lowest value in the second array. When you find the lowest value, save its index. You'll use the index to find the corresponding month:

int minIndex = 0;

for(int i = 1; i < rainfall.length; i++)
{
    if(rainfall[i] < rainfall[minIndex])
    {
        minIndex = i;
    }
}

string month = months[minIndex];
async
  • 1,537
  • 11
  • 28
-1

Pretty simple solution is to search for the index of your smallVal and bigVal.

for(int i = 0; i<rainfall.size(); i++){
   if(maxRain == rainFall[i])
         maxIndex = i;
   if(minRain == rainFall[i])
         minIndex = i;

}

smallMonth = months[minIndex];
bigMonth = months[maxIndex];
faraza
  • 416
  • 9
  • 21
  • It's O(n) all the way. Also, the algorithm is wrong. – async Sep 20 '15 at 08:54
  • The algorithm is not wrong. He says in the question that given the max value and min value in the array, he wants to find their index. – faraza Nov 09 '15 at 09:23