Find Second Largest Element in Unsorted Array

package org.wesome.dsalgo;

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

        /*  Find the firstLargest element */
        for (int indx = 0; indx < size; indx++) {
            firstLargest = Math.max(firstLargest, arr[indx]);
        }

        /*  Find the secondLargest element */
        for (int indx = 0; indx < size; indx++) {
            if (arr[indx] != firstLargest)
                secondLargest = Math.max(secondLargest, arr[indx]);
        }
        if (Integer.MIN_VALUE == secondLargest) {
            System.out.println("The first Largest is " + firstLargest + " There is no second Largest element");
            return -1;
        } else {
            System.out.println("The first Largest is " + firstLargest + " and the second Largest is " + secondLargest);
            return secondLargest;
        }
    }
}
package org.wesome.dsalgo;

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

import static org.wesome.dsalgo.SecondLargest.findSecondLargest;

public class SecondLargestTest {

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

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

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

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

    @Test
    public void secondLargestTestTest5() {
        int[] arr = {Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE};
        Assertions.assertEquals(-1, findSecondLargest(arr));
    }

    @Test
    public void secondLargestTestTest6() {
        int[] arr = {Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE};
        Assertions.assertEquals(-1, findSecondLargest(arr));
    }

    @Test
    public void secondLargestTestTest7() {
        int[] arr = {Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE};
        Assertions.assertEquals(-1, findSecondLargest(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