Java Array Max Value argmax(int[] a)

Here you can find the source of argmax(int[] a)

Description

argmax

License

Apache License

Declaration

public static int argmax(int[] a) 

Method Source Code

//package com.java2s;
/**/*ww w .j  a  v  a 2s . c  o  m*/
 * 
 *    Copyright 2017 Florian Erhard
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 */

public class Main {
    public static int argmax(int[] a) {
        int re = Integer.MIN_VALUE;
        int arg = -1;
        for (int i = 0; i < a.length; i++) {
            if (a[i] > re) {
                re = a[i];
                arg = i;
            }
        }
        return arg;
    }

    /**
     * Computes the argmax of the given array.
     * @param array the array
     * @return the argmax
     */
    public static int argmax(double[] array) {
        double max = array[0];
        int re = 0;
        for (int i = 1; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
                re = i;
            }
        }
        return re;
    }

    /**
     * Computes the argmax of the given array.
     * @param array the array
     * @param from start index (inclusive)
     * @param to end index (exclusive)
     * @return the argmax
     */
    public static int argmax(double[] array, int from, int to) {
        if (from >= to)
            return -1;
        double max = array[from];
        int re = from;
        for (int i = from + 1; i < to; i++) {
            if (array[i] > max) {
                max = array[i];
                re = i;
            }
        }
        return re;
    }

    /**
     * Computes the argmax of the given array.
     * @param array the array
     * @return the argmax
     */
    public static int argmax(float[] array) {
        float max = array[0];
        int re = 0;
        for (int i = 1; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
                re = i;
            }
        }
        return re;
    }
}

Related

  1. argmax(double[] array, boolean naturalNumbers)
  2. argmax(double[] weights)
  3. argmax(final double[] vec)
  4. argmax(float[] args)
  5. argMax(int[] a)
  6. argmax(int[] array)
  7. argmax(int[] array)
  8. argmax(int[] input)
  9. arrayMax(double maxVal, double[] vals)