vb数组怎么去除重复数据

vb
457
2023/10/20 18:44:46
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

可以使用以下方法去除重复数据:

  1. 使用List来存储数组中的数据,List的特点是可以自动去除重复元素。将数组中的元素逐个添加到List中,然后将List转换回数组。
Dim arr() As Integer = {1, 2, 3, 3, 4, 5, 5}
Dim list As New List(Of Integer)

For Each num As Integer In arr
    If Not list.Contains(num) Then
        list.Add(num)
    End If
Next

arr = list.ToArray()
  1. 使用HashSet来存储数组中的数据,HashSet的特点是自动去除重复元素。将数组中的元素逐个添加到HashSet中,然后将HashSet转换回数组。
Dim arr() As Integer = {1, 2, 3, 3, 4, 5, 5}
Dim set As New HashSet(Of Integer)

For Each num As Integer In arr
    set.Add(num)
Next

arr = set.ToArray()

这两种方法都可以去除数组中的重复数据,选择哪一种方法取决于你对List或HashSet的偏好。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: vb怎么复制文件到指定文件夹