Find Largest Element in Unsorted Array

package org.wesome.dsalgo;

public class LargestElement {
    static int findLargestElement(int[] arr) {
        if (arr.length < 1) {
            System.out.println(" array size is less then required");
            return -1;
        }
        int size = arr.length, largestElement = Integer.MIN_VALUE;

        /*  Find the largest Element element */
        for (int indx = 0; indx < size; indx++) {
            largestElement = Math.max(largestElement, arr[indx]);
        }
        System.out.println("The first Largest is " + largestElement);
        return largestElement;
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.wesome.dsalgo.LargestElement.findLargestElement;

public class FindLargestElementTest {

    @Test
    public void findLargestElementTest1() {
        int[] arr = {};
        Assertions.assertEquals(-1, findLargestElement(arr));
    }

    @Test
    public void findLargestElementTest2() {
        int[] arr = {1};
        Assertions.assertEquals(1, findLargestElement(arr));
    }

    @Test
    public void findLargestElementTest3() {
        int[] arr = {1, 2, 3, 4, 5};
        Assertions.assertEquals(5, findLargestElement(arr));
    }

    @Test
    public void findLargestElementTest4() {
        int[] arr = {1, 1, 1, 1, 1};
        Assertions.assertEquals(1, findLargestElement(arr));
    }

    @Test
    public void findLargestElementTest5() {
        int[] arr = {Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE};
        Assertions.assertEquals(Integer.MAX_VALUE, findLargestElement(arr));
    }

    @Test
    public void findLargestElementTest6() {
        int[] arr = {Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE};
        Assertions.assertEquals(Integer.MAX_VALUE, findLargestElement(arr));
    }

    @Test
    public void findLargestElementTest7() {
        int[] arr = {Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE};
        Assertions.assertEquals(Integer.MIN_VALUE, findLargestElement(arr));
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.4.1"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

follow us on