Go Program to Find the Second Largest Element in an Array

Introduction

Finding the second largest element in an array is a common problem in programming. It involves identifying the largest element and then finding the next largest element in the array. This guide will demonstrate how to write a Go program to find the second largest element in an array.

Problem Statement

Create a Go program that:

  • Prompts the user to enter the number of elements in an array.
  • Takes input for the elements of the array.
  • Finds and displays the second largest element in the array.

Example:

  • Input: Array elements: [10, 20, 4, 45, 99]
  • Output: Second largest element: 45

Solution Steps

  1. Import the fmt Package: Use import "fmt" to include the fmt package for formatted I/O operations.
  2. Write the Main Function: Define the main function, which is the entry point of every Go program.
  3. Input the Number of Elements: Use fmt.Scanln to take input from the user for the number of elements in the array.
  4. Input the Array Elements: Use a loop to input the elements of the array from the user.
  5. Find the Largest and Second Largest Elements: Iterate through the array to find the largest and second largest elements.
  6. Display the Second Largest Element: Use fmt.Println to display the second largest element.

Go Program

package main

import "fmt"

/**
 * Go Program to Find the Second Largest Element in an Array
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 1: Declare a variable to hold the number of elements in the array
    var n int

    // Step 2: Prompt the user to enter the number of elements
    fmt.Print("Enter the number of elements in the array: ")
    fmt.Scanln(&n)

    // Step 3: Declare an array to hold the elements
    array := make([]int, n)

    // Step 4: Input the elements of the array
    fmt.Println("Enter the elements of the array:")
    for i := 0; i < n; i++ {
        fmt.Scanln(&array[i])
    }

    // Step 5: Initialize variables to find the largest and second largest elements
    var largest, secondLargest int
    if array[0] > array[1] {
        largest = array[0]
        secondLargest = array[1]
    } else {
        largest = array[1]
        secondLargest = array[0]
    }

    // Step 6: Iterate through the array to find the largest and second largest elements
    for i := 2; i < n; i++ {
        if array[i] > largest {
            secondLargest = largest
            largest = array[i]
        } else if array[i] > secondLargest && array[i] != largest {
            secondLargest = array[i]
        }
    }

    // Step 7: Display the second largest element
    fmt.Println("Second largest element:", secondLargest)
}

Explanation

Step 1: Declare Variables

  • The variable n is declared to store the number of elements in the array.

Step 2: Input the Number of Elements

  • The program prompts the user to enter the number of elements using fmt.Print. The fmt.Scanln function reads the input and stores it in the n variable.

Step 3: Declare the Array

  • An array array is created using make to hold n integer elements.

Step 4: Input the Array Elements

  • The program uses a for loop to prompt the user to enter each element of the array, storing the input in the corresponding index of array.

Step 5: Initialize Variables

  • The program initializes two variables, largest and secondLargest, to store the largest and second largest elements, respectively. The first two elements of the array are compared to initialize these variables.

Step 6: Find the Largest and Second Largest Elements

  • The program uses a for loop starting from the third element of the array to find the largest and second largest elements. If an element is greater than largest, secondLargest is updated to the value of largest, and largest is updated to the current element. If an element is greater than secondLargest but less than largest, secondLargest is updated to the current element.

Step 7: Display the Second Largest Element

  • The program prints the value of secondLargest using fmt.Println.

Output Example

Example 1:

Enter the number of elements in the array: 5
Enter the elements of the array:
10
20
4
45
99
Second largest element: 45

Example 2:

Enter the number of elements in the array: 4
Enter the elements of the array:
12
35
1
10
Second largest element: 12

Conclusion

This Go program demonstrates how to find the second largest element in an array by iterating through the array and comparing elements. It covers basic programming concepts such as arrays, loops, and conditional statements. This example is useful for beginners learning Go programming and understanding how to manipulate arrays to find specific elements.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top