1

In the below code I am getting the following error on line If (Not hash.Add(Numbers(Num))) Then Value of Type 'Integer' cannot be converted to 'System.Array'. What am I doing wrong?

Module Module1

Sub Main()

    Dim array() As Integer = {5, 10, 12, 8, 8, 14}

    ' Pass array as argument.
    Console.WriteLine(findDup(array))

End Sub

Function findDup(ByVal Numbers() As Integer) As Integer

    Dim hash As HashSet(Of Array)

    For Each Num In Numbers

        If (Not hash.Add(Numbers(Num))) Then
            Return (Num)
        End If

    Next

End Function

End Module
1
  • 1
    In addition to the current error (which @shahkalpesh has answered), it's unlikely that you want to use Numbers(Num) in your call to Add, since Num is already a value extracted from the Numbers array. Commented Nov 11, 2011 at 15:28

4 Answers 4

2

Inside findDup, this

Dim hash As HashSet(Of Array)

Should be

Dim hash As HashSet(Of Integer)

EDIT: As suggested by @Damien_The_Unbeliever, the code

This line

If (Not hash.Add(Numbers(Num))) Then

Should be

If (Not hash.Add(Num)) Then
Sign up to request clarification or add additional context in comments.

2 Comments

Would still produce an Index Out of Bounds error because of how he is trying to add to the hash set during the loop
@D..: The editing was done while you were adding the above comment.
1

You have created a hashset of Array, rather than a hashset of Integer. You could change it to Integer, and alter how you try to add things in your loop to:

Function findDup(ByVal Numbers() As Integer) As Integer

    Dim hash As New HashSet(Of Integer)

    For Each Num In Numbers

        If (Not hash.Add(Num)) Then
            Return (Num)
        End If

    Next

End Function

I am hoping you realize that it will only ever find the first duplicate, and doesn't return a value of any type if it doesn't find a duplicate.

Comments

0

You've declared hash to be HashSet(Of Array), which means that it holds arrays. But you're trying to add integers to it.

You need to either change its declaration (HashSet(Of Integer)) or change the call to Add (Add(Numbers)).

Which solution you use will depend on your intent. My guess is that you want to change the type of hash.

Comments

0

Is this what you mean to do?

Sub Main()

    Dim someArray() As Integer = {5, 10, 12, 8, 7, 8, 8, 10, 14, 10}

    ' Pass array as argument.
    Dim foo As List(Of Integer) = findDups(someArray)
    'foo contains a list of items that have more than 1 occurence in the array
End Sub

Function findDups(ByVal Numbers() As Integer) As List(Of Integer)
    Dim rv As New List(Of Integer)
    For idx As Integer = 0 To Numbers.Length - 1
        If Array.LastIndexOf(Numbers, Numbers(idx)) <> idx Then
            If Not rv.Contains(Numbers(idx)) Then rv.Add(Numbers(idx))
        End If
    Next
    Return rv
End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.