Find matches in a card hand turns up slightly off results around 10% of the time - vb.net

This is my code which is supposed to compare the values in the array 'arrHands' which stores a hand of x cards (x = cardsDrawn) as singles where the integer part is the suit (1 to 4) and the decimal represents the card number ( .01 = 1 = Ace, etc).
However around 1 in 10 times it runs it returns values that are off by one or two pairs. I know that this will happen when the hand contains a three-of-a-kind, as i haven't written the code for that yet, but it still doesn't make sense. if the value returned is wrong it is always higher than i expected.
here's the code:
Dim numPairs As Integer = 0
Dim A As Integer = 1
Dim B As Integer = 1
'A and B represent the position in the array of the cards being compared
For A = 1 To cardsDrawn
For B = 1 To cardsDrawn
If (A <> B) And (A < B) And (arrHand(A) <> 0) And (arrHand(B) <> 0) Then
'The above line stops cards from being compared to each other, or to a card they have already been compared to.
If (arrHand(A) - (Int(arrHand(A))) = (arrHand(B) - (Int(arrHand(B))))) Then
'the code above extracts the card number from the single that each card is stored as
numPairs += 1
arrHand(A) = 0
arrHand(B) = 0
End If
End If
Next
Next
Thanks for any help or ideas you may have.

It is almost always a bad idea to glue 2 pieces of information into one variable. Classes make it easy to track the various datum for a card:
Public Class Card
Private Shared Faces() As String = {"Jack", "Queen", "King", "Ace"}
Private Shared Names() As String = {"Ace", "Deuce", ..."King"}
Public Property Value As Int32
Public Property Rank As Int32
Public Property Suit As String
Public Property Img As Image
Public Property Name As String
Public Overrides Function ToString() As String
Return String.Format("{0} of {1}", Names(Rank - 1), Suit)
End Function
End Class
There is a Value and a Rank because a card always has the same rank, but depending on the game, the Value may change (e.g. Euchre and Baccarat). A Deck class could use a Stack(Of Card) and a hand can be a List(Of Card) or an array depending on the game.
Given an array of 5 cards, ranking the hand for poker is simple with linq (and assuming Poker):
Dim pairs = cards.
GroupBy(Function(v) v.Value,
Function(key, values) New With {
Key .Rank = key,
Key .Count = values.Count()
}).
OrderByDescending(Function(o) o.Count).
ThenByDescending(Function(r) r.Rank).
ToArray()
If pairs.Count is 4, there is Four of a Kind; likewise 2pair when Count=2 and a pair when the count is one. If pairs(0).Count = 3 then you have trips.
If pairs.Count = 2 AndAlso pairs(0).Count = 3, then you have a FullHouse. It is pretty simple to also do a Group on the suit to determine a flush, and put them in order to see if it is a Straight.
Do be sure to test hands from high to low: you don't want to return 2 pair when it is really a Full House.

Related

How to get a collection of elements in my dictionary that meet a given condition?

I am going through some pluralsight videos and I found this amazing one called "Beautiful C++ 14: STL Alorithms". In this video the instructor discusses using the a variation of find_if methods like the one below.
vector<int> v{ 4, 6, 6, 1, 3, -2, 0, 11, 2, 3, 2, 4, 4, 2, 4 };
//find the first zero in the collection
auto result = find(begin(v), end(v), 0);
These methods are fantastic and I will be using it for some of my C projects, but I am currently working in a VB project where I need some kind of the same functionality.
I have a dictionary and I need to get certain objects out of it based on an if statement.
Structure Example_Struct
Public ExampleItem1 As Integer
Public ExampleItem2 As Integer
End Structure
Private Example_Dictionary As New Generic.Dictionary(Of String, Example_Struct)
Private Sub PopulateDictionaryWithDummyData()
Dim dummyData1 As Example_Struct
Dim dummyData2 As Example_Struct
dummyData1.ExampleItem1 = 1
dummyData1.ExampleItem2 = 1
dummyData1.ExampleItem1 = 5
dummyData1.ExampleItem2 = 5
Example_Dictionary.Add("Data1", dummyData1)
Example_Dictionary.Add("Data2", dummyData2)
End Sub
Private Sub LoadData()
PopulateDictionaryWithDummyData()
Dim stIdx As Integer
Dim myStruct As New Example_Struct
Dim item1 As Integer = 5
Dim item2 As Integer = 5
Dim count As Integer = Example_Dictionary.Count
For stIdx = 1 To count
' Get the stand data
myStruct = Example_Dictionary.ElementAt(stIdx).Value
If (myStruct.ExampleItem1 = item1 And myStruct.ExampleItem1 = item2) Then
' Do Something
End If
Next
End Sub
Above is some sample code to test with were I populate my Example_Dictionary with some dummy data. Then loop through the dictionary and put the comment where I need to do some things.
I would like my code to take the for loop out of the picture completley, just grab the "Example_Struct" that matches my conditions.
If there are more than one "Example_Struct" that matches my condition, I would need it to return a collection of these.
To filter the dictionary values based on a certain condition, you may use something like the following:
Dim myStructs = Example_Dictionary.Values.
Where(Function(x) x.ExampleItem1 = item1 AndAlso x.ExampleItem2 = item2).ToList()
Note that when dealing with logical operations, it's always a good idea to use the short-circuit logical operators (i.e., AndAlso instead of And and OrElse instead of Or). You can learn more about the difference in this guide:
Logical and Bitwise Operators in Visual Basic

unable to add items to a list in VB.NET

Good morning,
so i have received this homework for the summer where i have to create a program to store a list of movies and display them, but the problem is that there isn't a defined number of movie so i can't use the constant method i've always used, so i tried doing that with variables instead, but whenever i press the input button twice the app crashes and i get the error "Index over the matrix limits"
Here's the code in the module
Module Module1
Public Structure Film
Public Titolo As String
Public Autore As String
Public Incasso As Integer
Public Nazionalita As String
End Structure
Public i As Integer = 0
Public Flm(i) As Film
End Module
And here's the input part
Public Class frmInput
Private Sub btnInserisci_Click(sender As Object, e As EventArgs) Handles btnInserisci.Click
If IsNumeric(txtIncasso.Text) = False Then
MsgBox("L'incasso deve essere un valore numerico", MsgBoxStyle.Exclamation, "Attenzione")
ElseIf txtTitolo.Text = "" Or txtAutore.Text = "" Or txtNazionalita.Text = "" Then
MsgBox("Uno o piĆ¹ valori sono vuoti", MsgBoxStyle.Exclamation, "Attenzione")
Else
Flm(i).Titolo = txtTitolo.Text
Flm(i).Autore = txtAutore.Text
Flm(i).Incasso = txtIncasso.Text
Flm(i).Nazionalita = txtNazionalita.Text
i += 1
End If
End Sub
End Class
You should use a List(Of Film) to store the inputs received.
A generic List like that has no practical limits and can grow while you add elements to it
Public Flm As List(Of Film) = new List(Of Film)
....
Else
Dim f as Film = new Film()
f.Titolo = txtTitolo.Text
f.Autore = txtAutore.Text
f.Incasso = txtIncasso.Text
f.Nazionalita = txtNazionalita.Text
Flm.Add(f)
End If
A List(Of Film) could be used like it was an array
For x As Integer = 0 To Flm.Count -1 Step 1
Console.WriteLine("Film #" & x+1)
Console.WriteLine("Titolo = " & Flm(x).Titolo)
.....
Next
And of course you can iterate over it using a simpler foreach
For Each Film f in Flm
Console.WriteLine("Film #" & x+1)
Console.WriteLine("Titolo = " & f.Titolo)
.....
Next
Although others have mentioned using List, which would probably be appropriate, you also mentioned it was a homework task, so maybe you need, or have to, use the more traditional arrays as you have shown. Bearing this in mind, and also to let you know what your problem is. You are incrementing i but not the array.
Public i As Integer = 0
Public Flm(i) As Film
Thus, Flm is 0 to 0, one element.
You add to this, all is OK.
You increment i, good, i += 1
However, you don't then increment the array, Flm(). Incrementing i doesn't automatically increment the array, Flm().
You need to use: ReDim Preserve
Thus... Change:
Else
Flm(i).Titolo = txtTitolo.Text
to:
Else
ReDim Preserve Flm(i)
Flm(i).Titolo = txtTitolo.Text
Lastly, IsNumeric and MsgBox are remnants of the VB6 days, there are vb.net equivalents. Also, using i as a global/public variable is really not good. It's very common, standard, to use i in all local subroutines and functions for little loops etc, it gets used and lost. If you look at almost all examples of code, you'll see i being used as the integer counter.

Is it possible for a picture box to detect colour in another picture box and avoid it?

I am currently attempting to make a visual route planner interface for a local town.
The GUI is a map lacking in detail simply showing a green background with grey lines for the roads.
Is it possible for another picture box representing a vehicle to avoid and be unable to access the green coloured areas of the Map Picture box?
How can I make the car picture box follow certain routes along the map picture box that can be worked out through calculations?
We'll do this in a couple steps:
Build the map from the data.
Implement a flexible pathfinding algorithm.
Draw a human-friendly version of the map.
Wrap up with user interactions.
I'll enrich this answer as you progress so it stays relevant.
1
First thing, we'll manipulate data. A road map is basically a network of points (or nodes) linked by lines (which I'll call vertex).
The points have coordinates (so we can draw them later), and sometimes other values, like a name. They are every start and end of a road, and sometimes in-between. To keep things simple, the user will only be able to start and end on points.
Vertex have length and sometimes stuff like "difficulty", "max speed", etc. These stats will determinate which road is the most optimized for the user's trip. We can also make sure that the algorithm doesn't consider a vertex when the user doesn't want to go this way, or force him to go through a specific vertex if we choose so.
You'll find that I included a sample map by setting it up in the map's constructor. I included approximative coordinates and distances. You can change it any way you like. Here what the data represents:
2
I'm using the A* (read A STAR) algorithm here. You mentioned Dijkstra's algorithm sooner, which is nice, but for this purpose I'll stick to A*.
There are differences between these two methods. If you wikipedia them, you'll find these two images which, I think, show really well the main difference between the two:
Dijkstra's algorithm:
A* algorithm:
As you can see by yourself, both algorithm are doing a fine job.
I commented the code, but You'll probably have a ton of questions about all these things. I'll hang around to answer them.
To manipulate this algorithm, you have to know this:
the difficulty value of a vertex makes it seem longer to the algorithm. It's not necessary (which is why I put it to 1 everywhere), but if you want to compare a mountain road to a speedway or a road with a 30 km/h speed limit to another one where you can speed at 100 km/h, you can tweak this value.
the x and y coordinates of the nodes aren't very important in themselves, but the A* algorithm uses them to get this "I'm going toward my goal" effect you just saw in the image. Approximatives values are fine, as long as there are values that make some sense in there. You could calculate the H value otherwise, but as this is cartography I though it made sense.
a vertex MUST ALWAYS be connected to a starting point and an end point. Which is which have no meaning, as long as there are two of them (or the same point twice, but that would lead to the place where it started without any gain whatsoever).
a point may have any number of vertex.
you could make some points "impossible to pass", but I didn't code it that way. You can pump up the difficulty to an insane amount to get a somewhat similar result, except that if there are no other way the algorithm will eventually use the difficult path anyway.
if the end node has no connection to the network of the starting node, the algorithm will try everything then abandon and let you know that there is no path to the end point.
use the TestMyMap() sub to try some pathfinding. Calculate it by yourself to compare. Change the end point and the starting point for something else. Go wild!
The Code
#Region " Mapping "
Public Class MapPoint
Private _vertexList As New List(Of MapVertex) 'one point can have as many "roads" as you want
Private _xCoord As Decimal 'for now the coordinates are meaningless, but at some point we might want to use them to actually paint the map
Private _yCoord As Decimal
Private _name As String 'just a useful label
Public Parent As MapVertex = Nothing 'useful for backtracking at the end (and get an itinary)
'f is the total "cost" of the the node being analyzed (f = g + h) while doing pathfinding
'g is the distance between the node being analyzed and the starting point while doing pathfinding
'those values serve no other purpose
'h is the estimated minimal distance between the two points while doing pathfinding
'in the Sub 'CalculateH' I'm using the points coordinates and simple trigonometry to estimate h
'technically you can skip the square root part, but I decided against it. There are no dealmaking advantages to my decision.
'it's better to underestimate the minimal distance, that's why a theoretical straight line is a good choice
Public f As Decimal = 0
Public g As Decimal = 0
Public h As Decimal = 0
Public ReadOnly Property VertexList As List(Of MapVertex)
Get
Return _vertexList
End Get
End Property
Public ReadOnly Property X As Decimal
Get
Return _xCoord
End Get
End Property
Public ReadOnly Property Y As Decimal
Get
Return _yCoord
End Get
End Property
Public ReadOnly Property Name As String
Get
Return _name
End Get
End Property
Public Sub New(name As String, xx As Decimal, yy As Decimal, ParamArray vertex() As MapVertex)
_name = name
_xCoord = xx
_yCoord = yy
For Each v As MapVertex In vertex
_vertexList.Add(v)
Next
End Sub
Public Sub AddVertex(vertex As MapVertex)
_vertexList.Add(vertex)
End Sub
Public Sub CalculateH(startingPoint As MapPoint, endPoint As MapPoint)
h = Convert.ToDecimal(Math.Sqrt(Math.Pow((startingPoint.X - endPoint.X), 2) + Math.Pow((startingPoint.Y - endPoint.Y), 2)))
End Sub
Public Sub UpdateFandG(currentNodeG As Decimal, distance As Decimal)
g = currentNodeG + distance
f = g + h
End Sub
Public Sub ResetPathfindingValues()
f = 0
g = 0
h = 0
Parent = Nothing
For Each v As MapVertex In _vertexList
v.ResetPathfindingValues()
Next
End Sub
End Class
Public Class MapVertex
Private _name As String 'just a useful label
Private _length As Decimal 'length of the road. If you have straight roads we can use simple math to get the distances, but curvy roads have their own length, and I'll assume that your roads will be anything
Private _difficulty As Decimal 'if you want to make some roads faster than others, we can use a variable like this one. It could be something else, like "maxSpeed", but for a prototype "difficulty" will do fine
'I suggest making the difficulty a multiplier. It'll be easier to use that way (so a road twice faster would be a '0.5' and a road twice harder to use would be a '2.0')
Public Parent As MapPoint = Nothing 'useful for backtracking at the end (and get an itinary)
'start and end has no meaning here, as both are end points for the line. The important part is that they refer to existing points
'a vertex must ALWAYS be linked to two points (or it makes no sense, you cannot have a line with only one end unless you go full moebius and we're not using enough dimensions for this to make sense
'if you feel like it, you can have a vertex looping from one point and going back to it, but it'll serve no purpose and will never be actually used except by the pathfinding algorithm (which will note it as a bad move every time)
Private _startPoint As MapPoint
Private _endPoint As MapPoint
Public ReadOnly Property Name As String
Get
Return _name
End Get
End Property
'you can play around with difficulty if you like, it'll change which path the algorithm thinks is the better
Public ReadOnly Property AdjustedLength As Decimal
Get
Return _length * _difficulty
End Get
End Property
Public Sub New(name As String, startPoint As MapPoint, endPoint As MapPoint, length As Decimal, difficulty As Decimal)
_name = name
_startPoint = startPoint
_endPoint = endPoint
_length = length
_difficulty = difficulty
'automatically adding this vertex to it's points on creation:
_startPoint.AddVertex(Me)
_endPoint.AddVertex(Me)
End Sub
'this function is so we can get the "other side" of a path from it's start
Public Function GetOtherNode(currentNode As MapPoint) As MapPoint
If _startPoint Is currentNode Then
Return _endPoint
Else
Return _startPoint
End If
End Function
Public Sub ResetPathfindingValues()
Parent = Nothing
End Sub
End Class
Public Class Map
'the Map object is a collection of points.
'those points are linked by vertex
'it could be different depending on what's your purpose
Private _points As New List(Of MapPoint)
Public Function GetPoint(name As String) As MapPoint
For Each p As MapPoint In _points
If p.Name = name Then
Return p
End If
Next
Return Nothing
End Function
Public Sub New()
'You can use this part to build a simple map with a handful of points. For the demonstration that's how we'll work, but later on you can design a mean to load maps from a specific source if you want
'Here's one I threw on the screen at random. If you have one you like better, send me a pastebin and I'll use it instead
'First I create the points:
Dim oldBarn As MapPoint = New MapPoint("Old Barn", 0, 0)
Dim home As MapPoint = New MapPoint("Home", 0, 12)
Dim forest As MapPoint = New MapPoint("Forest", 0, 21)
Dim creepyDeadEnd As MapPoint = New MapPoint("Creepy Dead End", 0, 26)
Dim groceries As MapPoint = New MapPoint("Groceries", 11, 8)
Dim girlfriendsPlace As MapPoint = New MapPoint("Girlfriend's Place", 20, 8)
Dim friendsHome As MapPoint = New MapPoint("Friend's Home", 5, 13)
Dim bar As MapPoint = New MapPoint("The Foo's Bar", 32, 14) 'hehehe
'Second I create the roads between those points (as long as 2 points exist I could create the road, I just decided I would be systematic)
Dim smallRoad As MapVertex = New MapVertex("Small Road", oldBarn, home, 12, 1)
Dim oakStreet As MapVertex = New MapVertex("Oak Street", home, forest, 9, 1)
Dim pathway As MapVertex = New MapVertex("Pathway", forest, creepyDeadEnd, 5, 1)
Dim oldRoad As MapVertex = New MapVertex("Old Road", oldBarn, friendsHome, 17, 1)
Dim arlingtonStreet As MapVertex = New MapVertex("Arlington Street", home, groceries, 7, 1)
Dim fourthStreet As MapVertex = New MapVertex("4th Street", home, girlfriendsPlace, 12, 1)
Dim placeLittlefinger As MapVertex = New MapVertex("Place Littlefinger", groceries, girlfriendsPlace, 9, 1)
Dim cedarCreek As MapVertex = New MapVertex("Cedar Creek", forest, bar, 19, 1)
Dim alley As MapVertex = New MapVertex("Alley", friendsHome, groceries, 7, 1)
Dim mainStreet As MapVertex = New MapVertex("Main Street", groceries, bar, 22, 1)
Dim durnhamRoad As MapVertex = New MapVertex("Durnham Road", friendsHome, bar, 27, 1)
Dim secretRoad As MapVertex = New MapVertex("Secret Road", oldBarn, bar, 61, 1)
'Adding the points to the Map
_points.AddRange({oldBarn, home, forest, creepyDeadEnd, groceries, girlfriendsPlace, friendsHome, bar})
End Sub
'This is an implementation of the A* algorithm, which is a very popular pathfinding algorithm for simple grids/node networks
'You could use Dijkstra's algorithm if you like it better or are a Sapkowsky's fan, but I think A* will do a great job here
Public Function FindTheShortestPath(startingPoint As MapPoint, endPoint As MapPoint) As String
Dim openList As New List(Of MapPoint) 'nodes that are going to be analyzed soon
Dim closedList As New List(Of MapPoint) 'nodes that have been analyzed
'we always start the analysis... from the starting point of course!
openList.Add(startingPoint)
startingPoint.CalculateH(startingPoint, endPoint)
'as long as we haven't found the path to the end point, the algorithm will continue looking
'there are 2 ways to exit this function:
' #1 is finding the endPoint
' #2 is having nowhere left to go and still not finding the end point (which means that it's impossible)
While (openList.Count > 0)
'look for the lowest f in the openList
'this is our current node for the analysis
Dim currentNode As MapPoint = Nothing
For Each p As MapPoint In openList
If currentNode Is Nothing OrElse p.f < currentNode.f Then
currentNode = p
End If
Next
'remove the currentNode from the open list (it's being analyzed) and add to the closedList (no need to come back)
openList.Remove(currentNode)
closedList.Add(currentNode)
'check if we've reached the end node
If currentNode Is endPoint Then
'yay!
'now let's backtrack to get an itinary:
Dim path As String = BackTrack(endPoint)
ResetAllPathfindingVariables()
Return path
End If
'finding which nodes are connected to the Current node
'then analyzing it
For Each vertex As MapVertex In currentNode.VertexList
Dim nextNode As MapPoint = vertex.GetOtherNode(currentNode)
'only work on a node if it's out of the closedList
If Not closedList.Contains(nextNode) Then
'I'm tracking parents to make an itinary when this is finished
nextNode.CalculateH(currentNode, endPoint)
nextNode.UpdateFandG(currentNode.g, vertex.AdjustedLength)
'if it's not yet on the openList, now's time to add it there!
'(I know that a negative check with a 'Else' clause is stupid, but it's easier to read in this context)
If Not openList.Contains(nextNode) Then
openList.Add(nextNode)
nextNode.UpdateFandG(currentNode.g, vertex.AdjustedLength)
nextNode.Parent = vertex
vertex.Parent = currentNode
Else
'if this is a known node but we found a faster path to reach it, update it's parent
If currentNode.g + vertex.AdjustedLength < nextNode.g Then
nextNode.UpdateFandG(currentNode.g, vertex.AdjustedLength)
nextNode.Parent = vertex
vertex.Parent = currentNode
End If
End If
End If
Next
End While
ResetAllPathfindingVariables()
Return "No path was found."
End Function
Private Sub ResetAllPathfindingVariables()
For Each p As MapPoint In _points
p.ResetPathfindingValues()
Next
End Sub
'recursive function to show the path found by the algorithm
Private Function BackTrack(location As Object, Optional path As String = "") As String
If path <> "" Then path = " => " & path
Select Case True
Case TypeOf location Is MapPoint
Dim currentPoint As MapPoint = DirectCast(location, MapPoint)
path = currentPoint.Name & path
If currentPoint.Parent Is Nothing Then
Return path
Else
Return BackTrack(currentPoint.Parent, path)
End If
Case TypeOf location Is MapVertex
Dim currentVertex As MapVertex = DirectCast(location, MapVertex)
path = currentVertex.Name & path
If currentVertex.Parent Is Nothing Then
Return path
Else
Return BackTrack(currentVertex.Parent, path)
End If
End Select
Return ""
End Function
End Class
Private Sub TestMyMap()
_map = New Map()
Dim path As String = _map.FindTheShortestPath(_map.GetPoint("Home"), _map.GetPoint("The Foo's Bar"))
Console.WriteLine(path)
End Sub
#End Region
Of course I'll be there to help you understand this part. It's heavy, I know. It's still a lot of fun, especially once it's finished and you get to use it!

How to find the largest integer(s) between 3 integers

I would like to find the largest integer(s) between 3 integers.
I could do this by nesting If statements. Since I have further code to write however this would be long and untidy.
I was wondering if there was an easier way to find the largest integer(s) (including if let's say A and B are equal but both higher than C).
P.S Can you do this with 2-D arrays?
Use LINQ to do this:
Dim numbers() As Integer = {1, 3, 5}
Dim max As Integer = numbers.Max()
Debug.Write("Max number in numbers() is " & max.ToString())
Output:
Edited as per conversation with OP on wanting to know which genre was ranked the best.
When asked How do you get the data? OP responds with:
I have a text file containing movie|genre on every line. I read this and count which genre (out of 3) is the highest.
I have drafted up some code which reads from a text file and populates a class.
First let me show you the code:
Dim myFilms As New Films
Using sr As New IO.StreamReader("C:\films.txt")
Do Until sr.Peek = -1
Dim columns As String() = sr.ReadLine().Split(New Char() {"|"c}, StringSplitOptions.RemoveEmptyEntries)
'columns(0) = film name
'columns(1) = genre
myFilms.Add(New Film(columns(0), columns(1)))
Loop
End Using
If myFilms.Count > 0 Then
Dim bestGenre = myFilms.GetBestGenre()
'Go off and read the genre file based on bestGenre
End If
From the above code you can see the class Films being populated with a new Film. I then call a method from the Films class, but only if there are films to choose from. Let me show you the class structure for both these:
Film:
Public Class Film
Public Key As String
Public Sub New(ByVal filmName As String,
ByVal genre As String)
_filmName = filmName
_genre = genre
End Sub
Private _filmName As String
Public ReadOnly Property FilmName As String
Get
Return _filmName
End Get
End Property
Private _genre As String
Public ReadOnly Property Genre As String
Get
Return _genre
End Get
End Property
End Class
Films:
Public Class Films
Inherits KeyedCollection(Of String, Film)
Protected Overrides Function GetKeyForItem(ByVal item As Film) As String
Return item.Key
End Function
Public Function GetBestGenre() As String
Return Me.GroupBy(Function(r) r.Genre).OrderByDescending(Function(g) g.Count()).First().Key
End Function
End Class
I must note that although this code does work it may come unstuck if you have 2 or more genres which are joint top. The code still works however it only returns one of the genres. You may want to expand on the code to suit your needs based on that scenario.
Try something like this:
Dim max As Integer
max = integer1
If integer2 > max Then
max = integer2
End If
If integer3 > max Then
max = integer3
End If
Not many more ways that I can think of off the top of my head to do this.
Something along these lines will work for any number of integers.
Put the numbers into an array then use a For[...]Next statement to loop through the array comparing the current member with max. If max is lower, set it to the current member. When the loop terminates, max will contain the highest number:
Dim nums() As Integer = {1, 2, 3}
Dim max As Integer
For i = 0 To nums.Length - 1
If max < nums(i) Then
max = nums(i)
End If
Next

Fastest way to detect duplicate numbers on array vb.net 2005

I have this project that let user inputs 5 different numbers from 1 to 50. But I want to validate it before saving to DB that i will be 5 unique numbers. What's the best and fastest way to do this?
You can use HashSet(Of T) to check this:
Dim numbers As IEnumerable(Of Integer) = GetInputFromUser()
Dim hash As HashSet(Of Integer) = new HashSet(Of Integer)(numbers)
Dim unique As Boolean = hash.Count = numbers.Count()
This will be much more efficient than options requiring a sort + iteration.
Check this code
Private Function HasDuplicates(ByVal arr As Array) As Boolean
For i As Integer = 0 To arr.Length - 1
If Not arr(i) Is Nothing Then
Dim l As Integer = Array.LastIndexOf(arr, arr(i))
If l <> i Then Return True
End If
Next
Return False
End Function
Reed Copsey's suggestion to use hash sets was a good one (I hadn't worked with the HashSet class before).
But I then discovered that the IEnumerable class offers an extension method called Distinct that copies the unique values from a source IEnumerable to a target IEnumerable.
Borrowing the first line of Reed's sample code, here's the new sample VB.NET code:
Dim numbers As IEnumerable(Of Integer) = GetInputFromUser()
Dim isUnique As Boolean = (numbers.Distinct.Count = numbers.Count)
The variable isUnique is True if the numbers IEnumerable contains no duplicate values, or False if it contains one or more duplicate values.
Put in an array, sort it and check if elements 1,2 2,3 3,4 and 4,5 are different (in a loop).
Pseudocode:
integer numbers[50]
zeroarray(numbers, 50)
integer count = 0;
while (count < 5)
{
integer value = getinput()
if (value >= 1 and value <= 50)
if (numbers[value] = 0)
{
count = count + 1
numbers[value] = 1
}
else
reject duplicate
else
reject invalid
}
You can try this very simple method:
Filtering Arrays using LINQ
To simplifiy lets say the user inputs 5 different numbers from 0 to 49.
You can create a Boolean Array IsUsed(49) with 50 elements.
Then when the user input the value iInputNum=30 you can set IsUsed(30)=TRUE.
Next time, when the user input the second value iInputNum=7, you can set IsUsed(7)=TRUE
In this way you can check in a very fast way if the number was already inserted.
if IsUsed(iInputNum) then
'Skip the Input (put the right code here)
else
'Accept the number
IsUsed(iInputNum)=TRUE
'save iInputNum in the database
end if
Do not forget to clear the array after inserting all 5 numbers.
Remenber to put the right index in order to handle the number 1-50 (e not 0-49)
Here's an alternate solution, not sure how it compares, efficiency wise, to the other solutions, but it seems to work (uses LINQ).
Dim numbers As List<int> = getListOfNumbers()
Dim allUnique As Boolean = numbers.Distinct().Count() = numbers.Count()
Very late to the party, but what about something like this (C#, sorry)?
byte[] hits = new byte[51];
byte[] entries = new byte[] { 1, 12, 12, 32, 26, 49 };
foreach (var entry in entries)
{
hits[entry]++;
}
The elements in the hits array are automatically initialized to 0. When the foreach loop is complete, hits will contain the occurrence count for every number in entries. If any is greater than 1, you have dupes.