Go Tutorials

Go Programs

How to Remove Items from Set based on a Condition in Go


How to Remove Items from Set based on a Condition in Go ?

Answer

To remove items from a set (represented as a map) based on a condition in Go, you can iterate over the map and delete items that meet the condition.



✐ Examples

1 Remove Even Numbers from a Set

In this example,

  1. First, we create a map named numberSet to represent the set, with integer keys and boolean values: numberSet := map[int]bool{1: true, 2: true, 3: true, 4: true, 5: true};.
  2. Next, we iterate over the map using a for loop.
  3. Within the loop, we check if the current number (key) is even.
  4. If the number is even, we remove it from the map using the delete function.
  5. We print the updated map to verify that even numbers have been removed.

Go Program

package main
import "fmt"
func main() {
    numberSet := map[int]bool{1: true, 2: true, 3: true, 4: true, 5: true}
    fmt.Println("Original Set:", numberSet)
    for num := range numberSet {
        if num % 2 == 0 {
            delete(numberSet, num)
        }
    }
    fmt.Println("Set after removing even numbers:", numberSet)
}

Output

Original Set: map[1:true 2:true 3:true 4:true 5:true]
Set after removing even numbers: map[1:true 3:true 5:true]

2 Remove Strings Longer Than 4 Characters from a Set

In this example,

  1. First, we create a map named stringSet to represent the set, with string keys and boolean values: stringSet := map[string]bool{"apple": true, "banana": true, "orange": true, "kiwi": true};.
  2. Next, we iterate over the map using a for loop.
  3. Within the loop, we check if the length of the current string (key) is greater than 4.
  4. If the length is greater than 4, we remove the string from the map using the delete function.
  5. We print the updated map to verify that strings longer than 4 characters have been removed.

Go Program

package main
import "fmt"
func main() {
    stringSet := map[string]bool{"apple": true, "banana": true, "orange": true, "kiwi": true}
    fmt.Println("Original Set:", stringSet)
    for str := range stringSet {
        if len(str) > 4 {
            delete(stringSet, str)
        }
    }
    fmt.Println("Set after removing strings longer than 4 characters:", stringSet)
}

Output

Original Set: map[apple:true banana:true orange:true kiwi:true]
Set after removing strings longer than 4 characters: map[kiwi:true]

Summary

In this tutorial, we learned How to Remove Items from Set based on a Condition in Go language with well detailed examples.




More Go Sets Tutorials

  1. How to create an Empty Set in Go ?
  2. How to Initialize a Set in Go ?
  3. How to Get Length of a Set in Go ?
  4. How to create a Set of size N in Go ?
  5. How to create a Set of Numbers from 1 to N in Go ?
  6. How to create a Set of integers in Go ?
  7. How to create a Set of Strings in Go ?
  8. How to Access Items in a Set in Go ?
  9. How to get a Random Item in a Set in Go ?
  10. How to Iterate Over a Set in Go ?
  11. How to check if a Set is Empty in Go ?
  12. How to check if a Set is Not Empty in Go ?
  13. How to get Subset from a Set in Go ?
  14. How to check if a Specific Item is present in the Set in Go ?
  15. How to check if a Set contains all the items of Another Set in Go ?
  16. How to Sort Items of a Set in Go ?
  17. How to Copy a Set in Go ?
  18. How to add an Item to a Set in Go ?
  19. How to find Union of Two Sets in Go ?
  20. How to find Intersection of Two Sets in Go ?
  21. How to check if Two Sets are Equal in Go ?
  22. How to Convert a Set of Integers to a Set of Strings in Go ?
  23. How to Convert a Set of Strings to a Set of Integers in Go ?
  24. How to Convert a Set of Floats to a Set of Strings in Go ?
  25. How to Convert a Set of Strings to a Set of Floats in Go ?
  26. How to Filter Items of a Set based on a Condition in Go ?
  27. How to Remove Specific Item from a Set in Go ?
  28. How to Remove Items from Set based on a Condition in Go ?
  29. How to create a Set of Sets in Go ?