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


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 two-pointer approach.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Start the main() function. Inside the main() function, initialize the sorted array.

  • Step 3 − Now, calls the duplicatesRemove() function and pass the array to it.

  • Step 4 − Further, the resultant updated array after removing the duplicates is printed using the fmt.Println() function.

  • Step 5 − Now, create a duplicatesRemove() function that takes an array of integers. This function will return an integer array after removing the duplicates from the initial array.

  • Step 6 − This function firstly check if the length of the array is less than 2. If so, it returns the initial array since it cannot remove duplicates from an array of length less than 2.

  • Step 7 − Now, initialize two pointers as i and j to 0 and 1 respectively. Compare the positions and elements at i and j.

  • Step 8 − Finally, it returns a slice of the initial array containing only the unique elements up to the i+1 position.

Example

Following is the go language program to remove duplicates from a sorted array using two-pointer approach with iterative method

package main

import "fmt"

func main() {
   arr := []int{10, 20, 20, 20, 30, 30, 30, 40, 40}
   fmt.Println("Initial array:", arr)
   arr = duplicatesRemove(arr)
   fmt.Println("Updated array after removing duplicates:", arr)
}

func duplicatesRemove(arr []int) []int {
   n := len(arr)
   if n < 2 {
      return arr
   }
   i, j := 0, 1
   for j < n {
      if arr[i] == arr[j] {
         j++
      } else {
         i++
         arr[i] = arr[j]
         j++
      }
   }
   return arr[:i+1]
}

Output

Initial array: [10 20 20 20 30 30 30 40 40]
Updated array after removing duplicates: [10 20 30 40]

Using Two-Pointer Approach With Optimized Iterative Method

In this method, we will define a duplicatesRemove() function using iterative approach in optimized manner that is used to remove duplicates from a sorted array using two-pointer approach.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Start the main() function. Inside the main() function, initialize the sorted array.

  • Step 3 − Now, calls the duplicatesRemove() function and pass the array to it.

  • Step 4 − Further, the resultant updated array after removing the duplicates is printed using the fmt.Println() function.

  • Step 5 − Now, create a duplicatesRemove() function that takes an array of integers. This function will return an integer array after removing the duplicates from the initial array.

  • Step 7 − Now, initialize two pointers as i and j to 0 and 1 respectively. Compare the positions and elements at i and j. If they are not equal, increment i and copy the element at position j to position i. Then, increment j to move to the next element.

  • Step 8 − Continue this until j reaches the end of the array.

  • Step 9 − Finally, it returns a slice of the initial array containing only the unique elements up to the i+1 position.

Example

Following is go language program to remove duplicates from a sorted array using two-pointer approach with optimized iterative method

package main

import "fmt"

func main() {
   arr := []int{10, 10, 20, 30, 30, 30, 40, 50, 50}
   fmt.Println("Initial array:", arr)
   uniqueArr := duplicatesRemove(arr)
   fmt.Println("Updated array after removing duplicates:", uniqueArr)
}

func duplicatesRemove(arr []int) []int {
   i := 0
   for j := 1; j < len(arr); j++ {
      if arr[j] != arr[i] {
         i++
         arr[i] = arr[j]
      }
   }
   return arr[:i+1]
}

Output

Initial array: [10 10 20 30 30 30 40 50 50]
Updated array after removing duplicates: [10 20 30 40 50]

Conclusion

We have successfully compiled and executed a go language program to remove duplicates from a sorted array using two-pointer approach with iterative and optimized-iterative method along with two examples. In the first example, we have used the iterative method and in the second example, we have used the optimized-iterative method. They uses a for loop to iterate over the array, comparing each element with the previous element and removing it if it is a duplicate.

Updated on: 03-Apr-2023

400 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements