# duplicate elimination problem

• (3 Pages)
• 1
• 2
• 3

## 36 Replies - 6447 Views - Last Post: 10 October 2011 - 08:39 AMRate Topic: 2 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=250561&amp;s=d798d740cdb04bc1d33d4d01504d28c3&md5check=' + ipb.vars['secure_hash'], cur_rating: 3, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 charyl

• D.I.C Regular

Reputation: 4
• Posts: 324
• Joined: 29-June 11

# duplicate elimination problem

Posted 09 October 2011 - 04:23 PM

I looked for code snippets online to do this project, have not really found any elegant code snippets except for this one. I'm wondering if someone can explain this code snippet to me because it looks very simple and I like this one, but am confused on a couple of things.

My project is to use a one-dimensional array to solve following:
Read in 20 numbers, each of which is between 10 and 100 inclusive. As each number is read, display it in NumbersEnteredListBox and if it's not a duplicate of a number already read, diplay it in UniiqueValuesListBox. Provide "worst case" all 20 numbers are different. Use smalled possible array to solve problem.

```
Public Function RemoveDups(strings() As String) As String()
Dim old_i As Integer
Dim last_i As Integer
Dim result() As String

' Make the result array.
ReDim result(1 To UBound(strings))

' Copy the first item into the result array.
result(1) = strings(1)

' Copy the other items
last_i = 1
For old_i = 2 To UBound(strings)
If result(last_i) <> strings(old_i) Then
last_i = last_i + 1
result(last_i) = strings(old_i)
End If
Next old_i

' Remove unused entries from the result array.
ReDim Preserve result(1 To last_i)

' Return the result array.
RemoveDups = result
End Function

```

I want to understand what I'm learning so it will stick. What is Ubound? I realize I need to rewrite some of this to fit to what I'm trying to accomplish, but I want to understand it first. This is not in the book thus far. The codes I have found were sorry...horrible...I want to learn to write better with less words if possible?

Is This A Good Question/Topic? 0

## Replies To: duplicate elimination problem

### #2 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• Joined: 24-July 11

## Re: duplicate elimination problem

Posted 09 October 2011 - 04:39 PM

I googled UBound for you and here's the MSDN link:
http://msdn.microsof...f(v=vs.80).aspx

What that means to me is that it returns the highest value in a subscript.

You could also check if the listbox already contains a certain value with this:
```If ListBox1.Items.Contains(...)
```

Replace '...' with the value and then add your conditions.
HTH
J-Bo
Was This Post Helpful? 1

### #3 charyl

• D.I.C Regular

Reputation: 4
• Posts: 324
• Joined: 29-June 11

## Re: duplicate elimination problem

Posted 09 October 2011 - 05:06 PM

jimmyBo, on 09 October 2011 - 04:39 PM, said:

I googled UBound for you and here's the MSDN link:
http://msdn.microsof...f(v=vs.80).aspx

What that means to me is that it returns the highest value in a subscript.

You could also check if the listbox already contains a certain value with this:
```If ListBox1.Items.Contains(...)
```

Replace '...' with the value and then add your conditions.
HTH
J-Bo

Hey J-Bo sorry I haven't gotten back to you yet, I promise I will, school's out of control' I still have a 4 chptr test tonight and 2 design projects. They're trying to kill me, but they can't I won't die until it's my time !

here's what I have at this momment: I have a txtbox to enter 20 numbers, I have a list box to view the 20 numbers and another list box for #'s that are not duplicates and a submit button to submit numbers one at a time. My code's a little off at the momment. The (strings) are giving me errors. I only want to be able to enter numbers 1-100.

```Public Class DuplicateElimination

Private Sub DuplicateElimination_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim txtEnter As String()
Dim numbers_entered As Integer
Dim unique_numbers As Integer
Dim result() As String
' Make the result array.
ReDim result(0 To UBound(Strings))

' Copy the first item into the result array.
result(1) = Strings(1)

' Copy the other items
unique_numbers = 1

For old_i = 2 To UBound(Strings)
If result(unique_numbers) <> Strings(numbers_entered) Then
unique_numbers = unique_numbers + 1
result(unique_numbers) = Strings(numbers_entered)
End If
Next old_i

' Remove unused entries from the result array.
ReDim Preserve result(1 To unique_numbers)

' Return the result array.
RemoveDups = result
End Sub
End Class

```

This post has been edited by charyl: 09 October 2011 - 05:08 PM

Was This Post Helpful? 0

### #4 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• Joined: 24-July 11

## Re: duplicate elimination problem

Posted 09 October 2011 - 07:07 PM

charyl, on 10 October 2011 - 10:06 AM, said:

jimmyBo, on 09 October 2011 - 04:39 PM, said:

I googled UBound for you and here's the MSDN link:
http://msdn.microsof...f(v=vs.80).aspx

What that means to me is that it returns the highest value in a subscript.

You could also check if the listbox already contains a certain value with this:
```If ListBox1.Items.Contains(...)
```

Replace '...' with the value and then add your conditions.
HTH
J-Bo

Hey J-Bo sorry I haven't gotten back to you yet, I promise I will, school's out of control' I still have a 4 chptr test tonight and 2 design projects. They're trying to kill me, but they can't I won't die until it's my time !

here's what I have at this momment: I have a txtbox to enter 20 numbers, I have a list box to view the 20 numbers and another list box for #'s that are not duplicates and a submit button to submit numbers one at a time. My code's a little off at the momment. The (strings) are giving me errors. I only want to be able to enter numbers 1-100.

```Public Class DuplicateElimination

Private Sub DuplicateElimination_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim txtEnter As String()
Dim numbers_entered As Integer
Dim unique_numbers As Integer
Dim result() As String
' Make the result array.
ReDim result(0 To UBound(Strings))

' Copy the first item into the result array.
result(1) = Strings(1)

' Copy the other items
unique_numbers = 1

For old_i = 2 To UBound(Strings)
If result(unique_numbers) <> Strings(numbers_entered) Then
unique_numbers = unique_numbers + 1
result(unique_numbers) = Strings(numbers_entered)
End If
Next old_i

' Remove unused entries from the result array.
ReDim Preserve result(1 To unique_numbers)

' Return the result array.
RemoveDups = result
End Sub
End Class

```

I'd give this a shot (it worked for me):
```If thisvalue <= 100 then
'code to add it to the array
else
msgbox("This value is invalid.")
End If

```

This should help you.
Don't worry too much about the sketches, school is more important (but here I am typing this at school!). It's nice of you.
Regards,
J-Bo
Was This Post Helpful? 1

### #5 charyl

• D.I.C Regular

Reputation: 4
• Posts: 324
• Joined: 29-June 11

## Re: duplicate elimination problem

Posted 09 October 2011 - 08:32 PM

jimmyBo, on 09 October 2011 - 07:07 PM, said:

charyl, on 10 October 2011 - 10:06 AM, said:

jimmyBo, on 09 October 2011 - 04:39 PM, said:

I googled UBound for you and here's the MSDN link:
http://msdn.microsof...f(v=vs.80).aspx

What that means to me is that it returns the highest value in a subscript.

You could also check if the listbox already contains a certain value with this:
```If ListBox1.Items.Contains(...)
```

Replace '...' with the value and then add your conditions.
HTH
J-Bo

Hey J-Bo sorry I haven't gotten back to you yet, I promise I will, school's out of control' I still have a 4 chptr test tonight and 2 design projects. They're trying to kill me, but they can't I won't die until it's my time !

here's what I have at this momment: I have a txtbox to enter 20 numbers, I have a list box to view the 20 numbers and another list box for #'s that are not duplicates and a submit button to submit numbers one at a time. My code's a little off at the momment. The (strings) are giving me errors. I only want to be able to enter numbers 1-100.

```Public Class DuplicateElimination

Private Sub DuplicateElimination_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim txtEnter As String()
Dim numbers_entered As Integer
Dim unique_numbers As Integer
Dim result() As String
' Make the result array.
ReDim result(0 To UBound(Strings))

' Copy the first item into the result array.
result(1) = Strings(1)

' Copy the other items
unique_numbers = 1

For old_i = 2 To UBound(Strings)
If result(unique_numbers) <> Strings(numbers_entered) Then
unique_numbers = unique_numbers + 1
result(unique_numbers) = Strings(numbers_entered)
End If
Next old_i

' Remove unused entries from the result array.
ReDim Preserve result(1 To unique_numbers)

' Return the result array.
RemoveDups = result
End Sub
End Class

```

I'd give this a shot (it worked for me):
```If thisvalue <= 100 then
'code to add it to the array
else
msgbox("This value is invalid.")
End If

```

This should help you.
Don't worry too much about the sketches, school is more important (but here I am typing this at school!). It's nice of you.
Regards,
J-Bo

my computer shut down un expectedly and I lost everything, have to rebuild, guess I will stick to the code I posted as I lost all my notes that were in note pad....If you are at school now it's 11:30pm here you must be across the ocean? it's 3 hours diff. on the west coast...How exciting. Hope you're learning great things! Always wondered what the difference in our educational systems were like.I'll put the code in, wish I would have noticed that myself! I'll email you bought the sketches. You're awesome.
Was This Post Helpful? 1

### #6 charyl

• D.I.C Regular

Reputation: 4
• Posts: 324
• Joined: 29-June 11

## Re: duplicate elimination problem

Posted 09 October 2011 - 09:36 PM

I don't understand arrays as well as I need to! here are my errors: I've played with the code but the book is very vague and there are no examples. I've looked at a few tutorials, but I'm stumped. If you could also explain what these strings are actually saying or doing so I may better understand,maybe I can look in a different direction.

Error 1 Module 'Strings' cannot be used as a type. Line 16 DuplicateElimination\DuplicateElimination\frmDuplicateElimination.vb DuplicateElimination
Error 2 'Strings' is a type and cannot be used as an expression. Line 24 DuplicateElimination\DuplicateElimination\frmDuplicateElimination.vb DuplicateElimination

```Public Class DuplicateElimination

Private Sub DuplicateElimination_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lblNum As MessageBox
Dim txtEnterArray(20) As Integer
Dim numbers_entered As Integer
Dim unique_numbers As Integer
Dim result() As String

' Make the result array.

ReDim result(0 To UBound(Of Strings))
'code to add it to the array
If numbers_entered <= 100 Then
Else
MsgBox("This value is invalid.")
End If

' Copy the first item into the result array.
result(1) = Strings(1)

' Copy the other items
unique_numbers = 1

For numbers_entered = 2 To UBound(Strings)
If result(unique_numbers) <> Strings(numbers_entered) Then
unique_numbers = unique_numbers + 1
result(unique_numbers) = Strings(numbers_entered)

End If
Next numbers_entered
' Remove unused entries from the result array.
ReDim Preserve result(0 To unique_numbers)

' Return the result array.
RemoveDups = result

End Sub
End Class

```

Was This Post Helpful? 0

### #7 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• Joined: 24-July 11

## Re: duplicate elimination problem

Posted 09 October 2011 - 09:42 PM

Im in Australia, so its now 3:40 pm as I write this.
On the east coast, that is.

I'm awesome? I can't say I get that a lot.
Glad I could help :gunsmile:
Was This Post Helpful? 1

### #8 charyl

• D.I.C Regular

Reputation: 4
• Posts: 324
• Joined: 29-June 11

## Re: duplicate elimination problem

Posted 09 October 2011 - 09:43 PM

jimmyBo, on 09 October 2011 - 09:38 PM, said:

Im in Australia, so its now 3:40 pm as I write this.
On the east coast, that is.

Australia is on my bucket list! I want to see/visit one day! I understand it's very expensive to live there. But I hear the people are very friendly.
Was This Post Helpful? 0

### #9 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• Joined: 24-July 11

## Re: duplicate elimination problem

Posted 09 October 2011 - 09:45 PM

What I see is that line 16 is am Else statement, and line 24 is an integer reference. I don't see anything unusual here.
Also, after you show your MsgBox, add an Exit Sub before your End If.
HTH
Was This Post Helpful? 1

### #10 charyl

• D.I.C Regular

Reputation: 4
• Posts: 324
• Joined: 29-June 11

## Re: duplicate elimination problem

Posted 09 October 2011 - 09:46 PM

jimmyBo, on 09 October 2011 - 09:42 PM, said:

Im in Australia, so its now 3:40 pm as I write this.
On the east coast, that is.

I'm awesome? I can't say I get that a lot.
Glad I could help :gunsmile:

lol.
Was This Post Helpful? 0

### #11 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• Joined: 24-July 11

## Re: duplicate elimination problem

Posted 09 October 2011 - 09:47 PM

If I'm any indication, were very friendly here.
it's mote just the taxes and prices of fuel, housingthat are high.
But with the competing food companies, food prices are always dropping.
Was This Post Helpful? 1

### #12 charyl

• D.I.C Regular

Reputation: 4
• Posts: 324
• Joined: 29-June 11

## Re: duplicate elimination problem

Posted 09 October 2011 - 09:55 PM

jimmyBo, on 09 October 2011 - 09:45 PM, said:

What I see is that line 16 is am Else statement, and line 24 is an integer reference. I don't see anything unusual here.
Also, after you show your MsgBox, add an Exit Sub before your End If.
HTH

in line 16 the word (strings) is underlined in blue as though I'm suppose to use a different comment as well as 24. I tried using just the word string but the blue line doesn't remove, tried taking the word out. If I take the s off of (strings) it gives me this error

Error 1 'Public Function UBound(Array As System.Array, [Rank As Integer = 1]) As Integer' has no type parameters and so cannot have type arguments.
\DuplicateElimination\DuplicateElimination\frmDuplicateElimination.vb 16 37 DuplicateElimination
Was This Post Helpful? 0

### #13 charyl

• D.I.C Regular

Reputation: 4
• Posts: 324
• Joined: 29-June 11

## Re: duplicate elimination problem

Posted 09 October 2011 - 10:21 PM

ok, I could have this code wrong but this is what I've tried next. One error left, but I have not tried to debug yet either.

my error is this:
Error 1 'RemoveDups' is not declared. It may be inaccessible due to its protection level. \DuplicateElimination\DuplicateElimination\frmDuplicateElimination.vb 43 9 DuplicateElimination

I realize I need to declare this, but I'm confused as to where this code is writing to. Shouldn't it be going to the unique_numbers list box? (this list box should not have any duplicate numbers in it.

```
Public Class DuplicateElimination

Private Sub DuplicateElimination_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lblNum As MessageBox
Dim txtEnterArray(20) As Integer
Dim numbers_entered As Integer
Dim unique_numbers As Integer
Dim result() As String

' Make the result array.

ReDim result(0 To UBound(txtEnterArray))

'code to add it to the array
If numbers_entered <= 100 Then
Else
MsgBox("This value is invalid.")
Exit Sub
End If

' Copy the first item into the result array.
result(1) = txtEnterArray(0)

' Copy the other items
unique_numbers = 1

For numbers_entered = 2 To UBound(txtEnterArray)
If result(unique_numbers) <> txtEnterArray(numbers_entered) Then
unique_numbers = unique_numbers + 1
result(unique_numbers) = txtEnterArray(numbers_entered)

End If
Next numbers_entered

' Remove unused entries from the result array.
ReDim Preserve result(0 To unique_numbers)

' Return the result array.
RemoveDups = result

End Sub
End Class

```

Was This Post Helpful? 0

### #14 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• Joined: 24-July 11

## Re: duplicate elimination problem

Posted 09 October 2011 - 10:56 PM

I do believe it should, it's sayin the result is the value.
J-Bo
Was This Post Helpful? 1

### #15 charyl

• D.I.C Regular

Reputation: 4
• Posts: 324
• Joined: 29-June 11

## Re: duplicate elimination problem

Posted 09 October 2011 - 11:08 PM

I'm having difficulty with my messagebox now. I've tried a couple different things>

Error 1 'MessageBox' is a type and cannot be used as an expression. DuplicateElimination\DuplicateElimination\frmDuplicateElimination.vb 21 13 DuplicateElimination

```Public Class DuplicateElimination

Private Sub DuplicateElimination_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lblNum As MessageBox
Dim txtEnterArray(20) As Integer
Dim numbers_entered As Integer
Dim unique_numbers As Integer
Dim result() As String

' Make the result array.

ReDim result(0 To UBound(txtEnterArray))

'code to add it to the array
If numbers_entered <= 100 Then
Else
MessageBox = ("This value is invalid.")
Exit Sub
End If

' Copy the first item into the result array.
result(1) = txtEnterArray(0)

' Copy the other items
unique_numbers = 1

For numbers_entered = 2 To UBound(txtEnterArray)
If result(unique_numbers) <> txtEnterArray(numbers_entered) Then
unique_numbers = unique_numbers + 1
result(unique_numbers) = txtEnterArray(numbers_entered)

End If
Next numbers_entered

' Remove unused entries from the result array.
ReDim Preserve result(0 To unique_numbers)

' Return the result array.
'RemoveDups = result

End Sub
End Class

```

Was This Post Helpful? 0

• (3 Pages)
• 1
• 2
• 3

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }