5 Replies - 257 Views - Last Post: 28 April 2010 - 03:31 PM Rate Topic: -----

#1 codeforever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 31-March 10

Array help

Posted 28 April 2010 - 06:19 AM

hi
i am copying array a and array b to array c.
the only problem is i don't want duplicate numbers?
i tried doing this by looping array c and when a duplicate is found put a zero there but what i
actually wanted to do is delete the element instead, and shift the array up.
i also found that for some reason the number 11 still show's up twice?
Thank you,


Dim a() As Double = {1, 2, 3, 4, 5, 6, 7, 7, 9, 10, 11, 11, 13, 14, 15, 16, 17, 18, 19, 20}
    Dim b() As Double = {1, 2, 3, 4, 5, 6, 7, 7, 9, 10, 11, 11, 13, 14, 15, 16, 17, 18, 19, 20}
    Dim c() As Double


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim indexA, indexB As Integer
        Dim doneA, doneB As Boolean
        'Merge ascending arrays, with duplications. 
        ReDim c(39)

        indexA = 0
        indexB = 0
        doneA = False
        doneB = False
        'Loop over each element of the resulting array. 
        For indexC As Integer = 0 To 39
            'If a's element is less than b's, add a's to c. 
            If ((a(indexA) <= b(indexB)) And Not doneA) Or (doneB) Then
                c(indexC) = a(indexA)
                'Get next element from a. 
                If indexA < 19 Then
                    indexA = indexA + 1
                Else
                    doneA = True
                End If
            Else
                'Otherwise, add b's element to c. 
                c(indexC) = b(indexB)
                'Get next element from b 
                If indexB < 19 Then
                    indexB = indexB + 1
                Else
                    doneB = True
                End If
            End If
        Next

        Dim flag, i, count As Integer
        flag = 0
        count = 0
        For i = 0 To c.GetUpperBound(0) - 1
            If flag = 0 Then
                If (c(i) = c(i + 1)) Then
                    c(i + 1) = 0
                     'if a duplicate is found but a 0 in that position
                    flag = 1
                End If
            Else
                If (c(i) <> c(i + 1)) Then
                    flag = 0
                End If
            End If
        Next


        For k As Integer = 0 To 39
            lstout.Items.Add(c(k))
        Next
    End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Array help

#2 Recoil  Icon User is offline

  • D.I.C Regular

Reputation: 23
  • View blog
  • Posts: 275
  • Joined: 28-June 08

Re: Array help

Posted 28 April 2010 - 06:30 AM

If I am understanding correctly, each time you want to put an item in the array© you will have to loop through the entire array© to find out if the item is the same as any other row, if it is then continue with the next "indexC"
Was This Post Helpful? 0
  • +
  • -

#3 Guest_codeforever*


Reputation:

Re: Array help

Posted 28 April 2010 - 02:37 PM

kind off. but i want to create or modify array c to have no duplicate numbers.
my code copy's array a and array b and puts them into array c but array c contains duplicate numbers. i want to fix array c to remove any duplicate numbers in array c?
Was This Post Helpful? 0

#4 AdamSpeight2008  Icon User is online

  • MrCupOfT
  • member icon


Reputation: 1997
  • View blog
  • Posts: 8,807
  • Joined: 29-May 08

Re: Array help

Posted 28 April 2010 - 02:52 PM

Did this task is much simple if you use LINQ, in fact its one line.
c = a.Concat(B)/>.Distinct.ToArray

Was This Post Helpful? 1
  • +
  • -

#5 Guest_codeforever*


Reputation:

Re: Array help

Posted 28 April 2010 - 03:24 PM

Thank you, AdamSpeight2008, this is very helpful, but i don't understand the code you provided. can you please explain a little?
Was This Post Helpful? 0

#6 AdamSpeight2008  Icon User is online

  • MrCupOfT
  • member icon


Reputation: 1997
  • View blog
  • Posts: 8,807
  • Joined: 29-May 08

Re: Array help

Posted 28 April 2010 - 03:31 PM

The code is very self explanatory.

Take array A Concatenate (Combine) with array B give me only the Distictive (Unique) elements, than convert from an IEnumerable into an Array (At this point the code is executed).

Simples. :smartass:
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1