How to Remove Items from Set based on a Condition in Go - Step by Step Examples
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,
- 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};
. - Next, we iterate over the map using a
for
loop. - Within the loop, we check if the current number (key) is even.
- If the number is even, we remove it from the map using the
delete
function. - 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,
- 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};
. - Next, we iterate over the map using a
for
loop. - Within the loop, we check if the length of the current string (key) is greater than 4.
- If the length is greater than 4, we remove the string from the map using the
delete
function. - 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
- How to create an Empty Set in Go ?
- How to Initialize a Set in Go ?
- How to Get Length of a Set in Go ?
- How to create a Set of size N in Go ?
- How to create a Set of Numbers from 1 to N in Go ?
- How to create a Set of integers in Go ?
- How to create a Set of Strings in Go ?
- How to Access Items in a Set in Go ?
- How to get a Random Item in a Set in Go ?
- How to Iterate Over a Set in Go ?
- How to check if a Set is Empty in Go ?
- How to check if a Set is Not Empty in Go ?
- How to get Subset from a Set in Go ?
- How to check if a Specific Item is present in the Set in Go ?
- How to check if a Set contains all the items of Another Set in Go ?
- How to Sort Items of a Set in Go ?
- How to Copy a Set in Go ?
- How to add an Item to a Set in Go ?
- How to find Union of Two Sets in Go ?
- How to find Intersection of Two Sets in Go ?
- How to check if Two Sets are Equal in Go ?
- How to Convert a Set of Integers to a Set of Strings in Go ?
- How to Convert a Set of Strings to a Set of Integers in Go ?
- How to Convert a Set of Floats to a Set of Strings in Go ?
- How to Convert a Set of Strings to a Set of Floats in Go ?
- How to Filter Items of a Set based on a Condition in Go ?
- How to Remove Specific Item from a Set in Go ?
- How to Remove Items from Set based on a Condition in Go ?
- How to create a Set of Sets in Go ?