Programming Articles - Page 428 of 3366

Golang program to create a hash collection

Akhil Sharma
Updated on 03-Apr-2023 13:18:29

242 Views

In this article, we will write a Golang programs to create hash collections with the help of make function and map function. Hashmap stores key value pairs that can be accessed via index. In this program we will create a hash collection with the help of two examples. Syntax func make ([] type, size, capacity) The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments. Using Make Function In this method, we will create a hashmap with the help of a make ... Read More

Golang program to demonstrate a simple module

Akhil Sharma
Updated on 03-Apr-2023 13:17:34

218 Views

In this Go language article, we will write programs to demonstrate a simple module. A module in Go is called as the collection of packages namely we can call it packages and sub-packages where the root file will be called as go.mod. In this program we will demonstrate a module using three different examples. Syntax func make ([] type, size, capacity) The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments Example1: Golang program to demonstrate a simple module using basic printing ... Read More

Golang program to create a module with variables

Akhil Sharma
Updated on 04-Apr-2023 08:24:15

431 Views

In golang we can create a module with variable using const or var keywords, orelse we can even import the module . A module in Go is a group of packages. It helps in managing the distribution of the package. Algorithm Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and Output. Step 2 − In the first step, create three variables name, age and address using var keyword and assign these variables the desired values. Step 3 − Then, create a ... Read More

Golang program to perform binary search on a slice of custom structs

Akhil Sharma
Updated on 03-Apr-2023 13:14:13

980 Views

In this Golang article, we are going to perform binary search on a slice of custom structs using iterative and recursive method. Binary search is a search algorithm used to find the position of a specific value within a sorted sequence of elements. Algorithm Step 1 − First, we need to import the fmt package. Step 2 − Create a custom Person structure with string type name and integer type age. Step 3 − Now, create a bSearch() function that is used to perform binary search on a slice of Person struct. Step 4 − It initializes the low ... Read More

Golang program to check if a stack is empty

Akhil Sharma
Updated on 03-Apr-2023 13:13:45

296 Views

In this Golang article, we are going to check if a stack is empty using iterative and recursive method. A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle. Algorithm Step 1 − First, we need to import the fmt and strconv package. Step 2 − Create a stack structure with an item slice to store the stack elements. Step 3 − Now, define the functions, push(), pop() and peek() to perform the basic operations with the stack. Step 4 − push() adds the item to the end of the stack while pop() removes the last ... Read More

Golang program to reverse a stack

Akhil Sharma
Updated on 03-Apr-2023 13:13:14

255 Views

In this Golang article, we are going to reverse a stack using recursive and iterative method. A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle. Algorithm Step 1 − First, we need to import the fmt and strconv package. Step 2 − Create a stack structure with an item slice to store the stack elements. Step 3 − Now, define the functions, push(), pop(), peek(), isEmpty() and print() to perform the basic operations with the stack. Step 4 − push() adds the item to the end of the stack while pop() removes ... Read More

Golang program to find if there exists a pair of numbers in an array that add up to a given target sum using two pointer approach

Akhil Sharma
Updated on 03-Apr-2023 13:12:40

538 Views

In this Golang article, we are going to find if there exists a pair of numbers in an array that add up to a given target sum using two pointer approach with iterative and optimized-iterative method. An array is a collection of elements of the same data type, arranged in a contiguous block of memory, and accessed using an index or a subscript. Using Two Pointer Approach With Iterative Method In this method, we will define a pairWithGivenSum() function using iterative approach that is used to find if there exists a pair of numbers in an array that add up ... Read More

Golang program to find longest subarray with a given sum using two-pointer approach

Akhil Sharma
Updated on 03-Apr-2023 13:12:02

339 Views

In this Golang article, we are going to find longest subarray with a given sum using two-pointer approach with iterative and optimized-iterative method. An array is a collection of elements of the same data type, arranged in a contiguous block of memory, and accessed using an index or a subscript. Using Two-Pointer Approach With Iterative Method In this method, we will define a longestSubarray() function using iterative approach that is used to find longest subarray with a given sum using two-pointer approach. Algorithm Step 1 − First, we need to import the fmt package. Step 2 − Start the ... Read More

Golang program to remove duplicates from a sorted array using two-pointer approach

Akhil Sharma
Updated on 03-Apr-2023 13:11:30

839 Views

In this Golang article, we are going to remove duplicates from a sorted array using two-pointer approach with iterative and optimized-iterative method. An array is a collection of elements of the same data type, arranged in a contiguous block of memory, and accessed using an index or a subscript. A sorted array is an array in which the elements are arranged in a particular order, such as ascending or descending order. Using Two-Pointer Approach With Iterative Method In this method, we will define a duplicatesRemove() function using iterative approach that is used to remove duplicates from a sorted array using ... Read More

Golang program to find the maximum product of two numbers in an array using two pointer approach

Akhil Sharma
Updated on 03-Apr-2023 13:10:58

519 Views

In this Golang article, we are going to find the maximum product of two numbers in an array using two pointer approach with iterative and optimized-iterative method. An array is a collection of elements of the same data type, arranged in a contiguous block of memory, and accessed using an index or a subscript. Using Two Pointer Approach With Iterative Method In this method, we will define a productMax() function using iterative approach that is used to find the maximum product of two numbers in an array using two pointer approach. Algorithm Step 1 − First, we need to ... Read More

Advertisements