避免重复数组

时间:2019-09-04 18:25:39

标签: arrays vb.net

如何创建将接收2个数组并返回没有重复的数组的方法?

目前我对VB.NET的了解很低

这是我的代码

Imports System

Module Module1

    Public Class MergeNames
        Public Shared Function UniqueNames(names1() As String, names2() As String) As String()
            Throw New NotImplementedException()
        End Function
    End Class

    Public Sub Main()
        Dim names1() As String = {"Ava", "Emma", "Olivia"}
        Dim names2() As String = {"Olivia", "Sophia", "Emma"}
        Console.WriteLine(string.Join(", ", MergeNames.UniqueNames(names1, names2))) ' should print Ava, Emma, Olivia, Sophia
    End Sub

End Module

我希望在UniqueNames方法中仅获得Ava和Sophia

5 个答案:

答案 0 :(得分:3)

这些是简单的操作,没有理由使它们特定于字符串。您可以使用下面的通用函数来获取任何单一类型的任意两个集合中不同的项目集合。

Module Module1

    ' Ava, Emma, Olivia, Sophia
    Public Function GetDistinctItems(Of T)(items1 As IEnumerable(Of T), items2 As IEnumerable(Of T)) As IEnumerable(Of T)
        Return items1.Concat(items2).Distinct()
    End Function

    ' Emma, Olivia
    Public Function GetDuplicateItems(Of T)(items1 As IEnumerable(Of T), items2 As IEnumerable(Of T)) As IEnumerable(Of T)
        Return items1.Join(items2, Function(i) i, Function(i) i, Function(i, j) i)
    End Function

    ' Ava, Sophia
    Public Function GetUniqueItems1(Of T)(items1 As IEnumerable(Of T), items2 As IEnumerable(Of T)) As IEnumerable(Of T)
        Return items1.Concat(items2).Except(GetDuplicateItems(items1, items2))
    End Function

    ' Ava, Sophia
    Public Function GetUniqueItems2(Of T)(items1 As IEnumerable(Of T), items2 As IEnumerable(Of T)) As IEnumerable(Of T)
        Return items1.Concat(items2).GroupBy(Function(i) i).Where(Function(i) i.Count() = 1).Select(Function(i) i.Key)
    End Function

    Public Sub Main()
        Dim names1 = {"Ava", "Emma", "Olivia"}
        Dim names2 = {"Olivia", "Sophia", "Emma"}
        Console.WriteLine(String.Join(", ", GetDistinctItems(names1, names2)))
        ' or simply do it inline
        Console.WriteLine(String.Join(", ", names1.Concat(names2).Distinct()))

        Console.WriteLine(String.Join(", ", GetDuplicateItems(names1, names2)))
        Console.WriteLine(String.Join(", ", GetUniqueItems1(names1, names2)))
        Console.WriteLine(String.Join(", ", GetUniqueItems2(names1, names2)))
    End Sub

End Module

distinct的LINQ操作非常简单,它也非常适合内联到Console.Writeline中。

答案 1 :(得分:1)

您的答案与您的问题中的期望相矛盾:

  

我希望在UniqueNames方法中仅获得 Ava和Sophia

我想出的最短方法就是使用IEnumerable内置函数,如下所示:

Sub Main
    Dim names1 = {"Ava", "Emma", "Olivia"}
    Dim names2 = {"Olivia", "Sophia", "Emma"}

    Console.WriteLine(String.Join(", ", GetDistinctItems(names1, names2))) ' Returns "Ava, Emma, Olivia, Sophia"
    Console.WriteLine(String.Join(", ", GetUniqueItems(names1, names2))) ' Returns "Ava, Sophia"
End Sub

Public Function GetDistinctItems(Of T)(items1 As IEnumerable(Of T), items2 As IEnumerable(Of T)) As IEnumerable(Of T)
    Return items1.Union(items2)
End Function

Public Function GetUniqueItems(Of T)(items1 As IEnumerable(Of T), items2 As IEnumerable(Of T)) As IEnumerable(Of T)
    Return items1.Union(items2).Except(items1.Intersect(items2))
End Function

答案 2 :(得分:0)

请尝试以下操作:

Private Function CombineArray(ByVal Array1() As String, ByVal Array2() As String) As String()
    Dim myArray(Array1.Length + Array2.Length - 1) As String
    Array1.CopyTo(myArray, 0)
    Array2.CopyTo(myArray, Array1.Length)
    Return myArray
End Function

Private Function GetJustUnique(ByVal ArrayToCheck() As String) As String()
    Dim UniqueOnly() = ArrayToCheck.Where(Function(x) ArrayToCheck.Where(Function(y) x = y).Count() = 1).Distinct().ToArray
    Return UniqueOnly
End Function

Private Sub btnGetUnique_Click(sender As Object, e As EventArgs) Handles btnGetUnique.Click
    Dim names1() As String = {"Ava", "Emma", "Olivia"}
    Dim names2() As String = {"Olivia", "Sophia", "Emma"}
    Dim names3() As String = GetJustUnique(CombineArray(names1, names2))
End Sub

或最短的

Private Sub btnGetUnique_Click(sender As Object, e As EventArgs) Handles btnGetUnique.Click
    Dim names1() As String = {"Ava", "Emma", "Olivia"}
    Dim names2() As String = {"Olivia", "Sophia", "Emma"}
    'if you will use the result
    Dim UniqueOnly() = names1.Concat(names2).Where(Function(x) names1.Concat(names2).Where(Function(y) x = y).Count() = 1).Distinct().ToArray
    'if you just will write to console
    Console.WriteLine(String.Join(", ", names1.Concat(names2).Where(Function(x) names1.Concat(names2).Where(Function(y) x = y).Count() = 1).Distinct().ToArray)) 
End Sub

有关不带concat的linq的更多其他信息:

    'Dim ItemNames2InNames1 = From a In names2 Where (names1.Contains(a))
    'Return {Olivia, Emma}

    'Dim ItemNames2NotInNames1 = From a In names2 Where (Not names1.Contains(a))
    'Return {Sophia}

    'Dim ItemNames1InNames2 = From a In names1 Where (names2.Contains(a))
    'Return {Olivia, Emma}

    'Dim ItemNames1NotInNames2 = From a In names1 Where (Not names2.Contains(a))
    'Return {Ava}

答案 3 :(得分:0)

Imports System
Imports System.Collections.Generic
Imports System.Linq

Public Module MergeNames
    Public Shared Function UniqueNames(names1 As IEnumerable(Of String), names2 As IEnumerable(Of String)) As IEnumerable(Of String)
        Return names1.Except(names2).Concat(names2.Except(names1))
    End Function
End Module

Module Module1

    Public Sub Main()
        'Can still use arrays here, even though the method now asks for IEnumerable(Of String)
        Dim names1() As String = {"Ava", "Emma", "Olivia"}
        Dim names2() As String = {"Olivia", "Sophia", "Emma"}
        Console.WriteLine(string.Join(", ", MergeNames.UniqueNames(names1, names2))) ' should print Ava, Emma, Olivia, Sophia
    End Sub
End Module

答案 4 :(得分:-1)

Imports System
Imports System.Collections

Module Module1

Public Class MergeNames
    Public Shared Function UniqueNames(names1() As String, names2() As String) As String()
        Dim result as New ArrayList
        Dim cont as Integer = 0

        'Dim aux As New List(Of String)

        Dim aux As Hashtable = New Hashtable

        For Each n1 as String in names1
            If Not(aux.Contains(n1)) Then
                aux.Add(n1, "")
                result.add(n1)
            End If
        Next

        For Each n2 as String in names2
            If Not(aux.Contains(n2)) Then
                aux.Add(n2, "")   
                result.add(n2)
            End If
        Next

        Dim res() As String = result.ToArray(GetType(String))

        Return res
    End Function
End Class

Public Sub Main()
    Dim names1() As String = {"Ava", "Emma", "Olivia"}
    Dim names2() As String = {"Olivia", "Sophia", "Emma"}
    Console.WriteLine(string.Join(", ", MergeNames.UniqueNames(names1, names2))) ' should print Ava, Emma, Olivia, Sophia
End Sub
End Module

让它自己工作,谢谢堆栈!