Don't miss your Year-End Offer: Up to 20% OFF

Find the Largest Number in Array

13 February 2024
public class HackerAlgo {

    public static int findLargestNumber(int[] arr) {
        if (arr.length == 0) {
            throw new IllegalArgumentException("Array is empty");
        }

        int max = arr[0];
        for (int num : arr) {
            if (num> max) {
                max = num;
            }
        }
        return max;
    }

    public static void main(String[] args) {
int[] exampleArr = {1, 5, -3, 2, 0, -8, 4};
        int result = findLargestNumber(exampleArr);
System.out.println(result
def find_largest_number(arr):
    if not arr:
        raise ValueError("Array is empty")

max_num = arr[0]
    for num in arr:
        if num>max_num:
max_num = num
    return max_num

# Example usage
example_arr = [1, 5, -3, 2, 0, -8, 4]
result = find_largest_number(example_arr)
print(result
#include <iostream>
#include <vector>

class HackerAlgo {
public:
    static int findLargestNumber(const std::vector<int>&arr) {
        if (arr.empty()) {
            throw std::invalid_argument("Array is empty");
        }

        int max_num = arr[0];
        for (int num : arr) {
            if (num> max_num) {
max_num = num;
            }
        }
        return max_num;
    }
};

int main() {
    // Example usage
std::vector<int>exampleArr = {1, 5, -3, 2, 0, -8, 4};
    int result = HackerAlgo::findLargestNumber(exampleArr);
std::cout<< result << std::endl;

    return 0
class HackerAlgo {
    static findLargestNumber(arr) {
        if (!arr.length) {
            throw new Error("Array is empty");
        }

        let maxNum = arr[0];
        for (let num of arr) {
            if (num> maxNum) {
maxNum = num;
            }
        }
        return maxNum;
    }
}

// Example usage
constexampleArr = [1, 5, -3, 2, 0, -8, 4];
const result = HackerAlgo.findLargestNumber(exampleArr);
console.log(result

Leave a Comment