Visual Basic.NET - matrices

 
Vista:

matrices

Publicado por henry lara (4 intervenciones) el 07/08/2007 19:37:48
¿como saco el numero que mas se repite de una matriz o de un vector? y en caso de que no se repita ninguno mostrar mensaje
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:matrices

Publicado por Harold V. (411 intervenciones) el 07/08/2007 21:43:12
Aqui va este codigo.........

'1,2,3,4,5,6,7,9,23,45,54,87 = 12 numeros diferentes
Dim Numbers() As Integer = {1, 2, 3, 4, 5, 6, 7, 9, 45, 1, 1, 1, 1, 1, 87, 4, 23, 54, 23, 1, 4, 7, 4, 3, 3, 2, 45}

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

Dim Numberfounds As New Dictionary(Of Integer, Integer)
Dim rep As Integer = 0
Dim num As Integer = 0
Dim _mayor As Integer = 0
Dim numero_rep As Integer = 0
Dim cant_veces As Integer = 0

'Ordenamos el arreglo
Array.Sort(Numbers)

For x As Int32 = 0 To Numbers.GetUpperBound(0) - 1
'obtenemos el valor del arreglo
num = Numbers.GetValue(x)
'Si no existe el indice en numberfounds arreglo lo insertamos
If Not Numberfounds.ContainsKey(num) Then
Numberfounds.Add(num, 1)
Else 'Aqui existe el numnero en el nuevo arreglo entonces sumamos 1 a su contador
rep = Numberfounds.Item(num)
Numberfounds.Item(num) = rep + 1

If Numberfounds.Item(num) >= _mayor Then
_mayor = Numberfounds.Item(num)
End If
End If
Next

'Recorremos nuestro diccionario mostrando los valores
For Each kvp As KeyValuePair(Of Integer, Integer) In Numberfounds
If kvp.Value > 1 Then 'numero con repeticion > 1
MessageBox.Show(kvp.Key & " Se repite " & kvp.Value & " veces.")
Else
MessageBox.Show(kvp.Key & " " & " Solo se repite una vez")
End If

'Numero mas rpetido
If kvp.Value = _mayor Then
numero_rep = kvp.Key
cant_veces = kvp.Value
End If

Next

MessageBox.Show("EL numero mas repetido fué el: " & numero_rep & " con " & cant_veces & " repeticiones.")

End Sub
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar