0

Ok so I'm trying to find the largest element in an array, and I realize this is not the best method to do this, it only works in some cases. Would appreciate some pointers on how to change my code so it works for all instances.

public static int maxArray(int[] a) {
    int max = 0;
    for (int j = 0; j < a.length-1; j++) {
        if (a[j+1] > a[j]) {
            max = a[j+1];
        } else {
            max = a[j];
        }

    }
    return max;

}
George12
  • 95
  • 1
  • 1
  • 7
  • Here is related question [link1](http://stackoverflow.com/questions/1484347/java-max-min-value-in-an-array) and [link2](http://stackoverflow.com/questions/16325168/how-would-i-find-the-maximum-value-in-an-array) – Kei Minagawa Mar 10 '14 at 23:33

6 Answers6

2

I suggest you do it like so,

Start with max = a[0]; then loop with j from 1 to a.length. Compare a[j] to max, that is if a[j] > max then set max = a[j];.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 2
    The only other suggestion I have is to handle null and the empty array. That is `if (a == null || a.length == 0) return null;` – Elliott Frisch Mar 10 '14 at 23:26
2

Use this method.

public static int maxArray(int[] a) {

int max = a[0]; // saves a bit of time

for (int j = 1; j < a.length; j++) {
    if (a[j] > max) {
        max = a[j];
    }

}
return max;

}

This is pretty speedy and concise.

ElectronicGeek
  • 3,312
  • 2
  • 23
  • 35
  • I would start your loop at `j = 1`, loop until `j < a.length`, and check if `a[j] > max`. – PlasmaPower Mar 10 '14 at 23:26
  • actually by adding the if check, you have made the `saves a bit of time` a bit more expensive than starting at index `0` and using `Integer.MIN_VALUE` as initial max – Max Fichtelmann Mar 10 '14 at 23:35
1

You need to compare the current element with maximum element, not with the next one.

if (a[j] > max) {
    max = a[j];
}
Warlord
  • 2,798
  • 16
  • 21
1

In Java 8 You can use Stream:

public static int maxArray(int[] a) {
    return Arrays.stream(a).max().getAsInt();
}
MWS
  • 11
  • 3
0
public static <T extends Object & Comparable<? super T>> T maxArray(T[] array) {
    return Collections.max(Arrays.asList(array));
}

after you make calls to the function, eg:

Integer[] a = { 1, 5, -6, 3, 0, 2 };

Integer max = maxArray(a);

System.out.println(max);

Community
  • 1
  • 1
OMARoun
  • 23
  • 4
  • Note that this is an `int[]` and not a `T[]`. This will not work. Also note that the `Object` in `T extends Object & Comparable super T>` is redundant. – Boris the Spider Mar 10 '14 at 23:46
  • for redundancy, the declaration of the max function is: `public static > T max(Collection extends T> coll)` – OMARoun Mar 10 '14 at 23:55
  • I assume you copied this from the [source of `Collections`](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Collections.java#Collections.max%28java.util.Collection%29). It is there for [backwards compatibility](http://stackoverflow.com/a/19488411/2071828). There is no reason to have it in your example. – Boris the Spider Mar 11 '14 at 00:05
  • yes, `` and add a cast to return : `(T)` – OMARoun Mar 11 '14 at 00:14
0
public static int getMaxElement(int[] elements) {
    int max = elements[0];
    for (int j = 0; j < elements.length-1; j++) {
        if (elements[j] > max) {
            max = elements[j];
        }
    }
    return max;
}
amunozdv
  • 111
  • 3