-3

I have two arrays with different sizes, say a1[5] and a2[7]. I want to compare the two arrays and find the largest integer. For example

Input

a1 = {52,53,48,72,20}
a2 = {28,12,73,21,33,44,88}

Output should be like a2, 88 where 88 is the largest number and a2 is the array where the largest number resides.

I did search in Google, but couldnt get anything. Just want to know is there any builtin function available for this? or give me some logic to get the output.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
sparkey
  • 154
  • 3
  • 13
  • 3
    why can't you just loop through the two arrays, having two variables, one that stores the largest number found, and then also one that contains the variable it came from. – MZimmerman6 Sep 17 '15 at 16:18
  • 1
    possible duplicate of http://stackoverflow.com/q/1484347/1768232 (yes, I'm out of close votes) – durron597 Sep 17 '15 at 16:20
  • 2
    Find the largest int in each array and compare them... – assylias Sep 17 '15 at 16:27
  • @durron597 I dont think so my question is duplicate. I did saw the link you shared before posting this question and that was finding the largest number in a single array. My question was in multiple arrays. Are you still seeing my question is duplicate? – sparkey Sep 17 '15 at 16:29
  • 1
    @PadmanabhanVijay And you can't figure out how to use that information to get the answer? – durron597 Sep 17 '15 at 16:29
  • @durron597 I am just a beginner dude. I am learning Java. – sparkey Sep 17 '15 at 16:30

4 Answers4

2

This should guide you in the right direction:

public class Answer{

    public double d;
    public int array;

    Answer(double d, boolean inFirstArray){
      if inFirstArray{
        this.d = d;
        this.int = 1;
      }
      else{
        this.d = d;
        this.int = 2;
      }
    }



}



public Answer findLargestNumber(double[] a1, double[] a2){

  boolean inFirstArray = true;
  double biggestNumber = 0;

  for (double d : a1){
    if (d > biggestNumber)
      biggestNumber = d;
  }

  for (double d : a2){
    if (d > biggestNumber){
      biggestNumber = d;
      inFirstArray = false;
    }
  } 

  Answer answer = new Anwser(biggestNumber,inFirstArray);
  return answer;  
}
Hips
  • 244
  • 1
  • 9
1

Just iterate through both arrays, find the greater value and the array it is contained;

Integer answer = Integer.MIN_VALUE;
Integer arr = 1;
Integer[] a1 = {1,2,4};
Integer[] a2 = {1,4,6,8};
for (Integer val : a1) {
   if (val > answer) {
      answer = val;
      arr = 1;
   }
}
for (Integer val : a2) {
   if (val > answer) {
      answer = val;
      arr = 2;
   }
}
System.out.println("a" + arr + "," + answer);
brlaranjeira
  • 367
  • 2
  • 11
1

As far as the logic goes: I would suggest you create a method which takes an array and returns the largest number in the array. You can call this function twice like this:

int maxValueInFirstArray = maxValueInArray(int[] firstArray);
int maxValueInSecondArray = maxValueInArray(int[] secondArray);

Then you can check the two value in an if statement to calculate the largest value. This will not only be shorter but easily readable. As far as the how the function maxValueInArray() works, I will leave that on you to find. I hope this helps :)

thegauravmahawar
  • 2,802
  • 3
  • 14
  • 23
1

This could be one way to find the largest integer from two arrays and the array where the largest number reside.

private static void findTheLargestInteger(int[] arr1, int[] arr2) {
    Arrays.sort(arr1);
    Arrays.sort(arr2);
    if(arr1[arr1.length-1] > arr2[arr2.length-1])
        System.out.println("a1, "+ arr1[arr1.length-1]);
    else
        System.out.println("a2, "+ arr2[arr2.length-1]);

}
sifho
  • 645
  • 5
  • 10