find smallest number in array java code example

Example 1: javascript removing smallest number in array

function removeSmallest(arr) {
    var min = Math.min(...arr);
    return arr.filter(e => e != min);
}

Example 2: java find biggest number in array

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

System.out.println("The highest maximum for the December is: " + max);

Example 3: javascript find the min in array of numbers

// Assuming the array is all integers,
// Math.min works as long as you use the spread operator(...).

let arrayOfIntegers = [9, 4, 5, 6, 3];
let min = Math.min(...arrayOfIntegers);
// => 3

Example 4: find the largest and smallest number in an unsorted integer array in Java

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Number of integers to enter:");
    int numberEnter = keyboard.nextInt();
    int numbers[] = new int[numberEnter];
    int pos = 0;
    do {
        System.out.printf("Please enter integer #%d/%d:%n", pos, numberEnter);
        numbers[pos++] = keyboard.nextInt();
    } while (pos < numberEnter && keyboard.hasNextInt());
    int min = numbers[0];
    int max = numbers[0];
    for (pos = 1; pos < numbers.length; pos++) {
        if (numbers[pos] < min) { // <-- test min.
            min = numbers[pos];
        }
        if (numbers[pos] > max) { // <-- test max.
            max = numbers[pos];
        }
    }
    // Display everything.
    System.out.printf("%s Min: %d Max: %d%n", Arrays.toString(numbers),
            min, max);
}

Example 5: how to find the smallest numbers in an arraylist java

import java.util.ArrayList;
 
import java.util.Scanner;
 
public class IndexOfSmallest {
 
    public static void main(String[] args) {
 
        Scanner scanner = new Scanner(System.in);
      
        ArrayList list = new ArrayList<>();
 
        while (true) {
 
            int input = Integer.valueOf(scanner.nextLine());
 
            if (input == 9999) {
 
                break;
 
            }
 
            list.add(input);
 
        }
 
        System.out.println("");
 
        int smallest = list.get(0);
 
        int index = 0;
 
        while (index < list.size()) {
 
            if (list.get(index) < smallest) {
 
                smallest = list.get(index);
 
            }
 
            index++;
 
        }
 
        System.out.println("Smallest number: " + smallest);
 
        index = 0;
 
        while (index < list.size()) {
 
            if (list.get(index) == smallest) {
 
                System.out.println("Found at index: " + index);
 
            }
 
            index++;
 
        }
 
    }
 
}

Tags:

Misc Example