求一函数(获取数组中指定重出现次数的元素集)
求一函数(获取数组中指定重出现次数的元素集)
如:a=1,2,2,3,3,4,4,4,5,5,5,6,6,6
指定取重复两次的元素:返回结果=2,3
[最优解释]
Imports System.IO
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim arr As Integer() = {1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6}
Dim dict As Dictionary(Of Integer, Integer) = GetRepeat(arr, 2)
For Each key As Integer In dict.Keys
Console.WriteLine(key.ToString() + "重复了" + dict(key).ToString() + "次")
Next
Console.ReadKey()
End Sub
Function GetRepeat(ByVal arr As Integer(), ByVal repeat As Integer) As Dictionary(Of Integer, Integer)
Dim dict As New Dictionary(Of Integer, Integer)
For Each i As Integer In arr
If dict.ContainsKey(i) Then
dict(i) = dict(i) + 1
Else
dict.Add(i, 1)
End If
Next
Dim result As New List(Of Integer)
For Each key As Integer In dict.Keys
If dict(key) <> repeat Then result.Add(key)
Next
For Each key As Integer In result
dict.Remove(key)
Next
Return dict
End Function
End Module
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim arr As Integer() = {1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7}
Console.WriteLine(GetValue(arr, 2))
End Sub
Private Function GetValue(ByVal arr() As Integer, ByVal count As Integer) As String
Dim value As String = String.Empty
Dim dict As New Dictionary(Of Integer, Integer)
For Each i As Integer In arr
If dict.ContainsKey(i) Then
dict(i) = dict(i) + 1
Else
dict.Add(i, 1)
End If
Next
For Each key As Integer In dict.Keys
If dict(key) = count Then
value &= key & ","
End If
Next
Return value.Trim(",")
End Function
private void Form1_Load(object sender, EventArgs e)
{
int[] arr = new int[] { 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7 };
Console.WriteLine(GetValue(arr,2));
}
private string GetValue( int[] arr,int count)
{
string value=string.Empty;
Dictionary<int, int> dict = new Dictionary<int, int>();
foreach (int i in arr)
{
if (dict.ContainsKey(i)) dict[i]++;
else dict.Add(i, 1);
}
foreach (int key in dict.Keys)
{
if (dict[key] == count)
{
value += key+",";
}
}
return value.Trim(',');
}
Imports System.IO
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim arr As Integer() = {1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6}
Dim dict As Dictionary(Of Integer, Integer) = GetRepeat(arr, 2, 3)
For Each key As Integer In dict.Keys
Console.WriteLine(key.ToString() + "重复了" + dict(key).ToString() + "次")
Next
Console.ReadKey()
End Sub
Function GetRepeat(ByVal arr As Integer(), ByVal repeatFrom As Integer, ByVal repeatTo As Integer) As Dictionary(Of Integer, Integer)
Dim dict As New Dictionary(Of Integer, Integer)
For Each i As Integer In arr
If dict.ContainsKey(i) Then
dict(i) = dict(i) + 1
Else
dict.Add(i, 1)
End If
Next
Dim result As New List(Of Integer)
For Each key As Integer In dict.Keys
If dict(key) < repeatFrom Or dict(key) > repeatTo Then result.Add(key)
Next
For Each key As Integer In result
dict.Remove(key)
Next
Return dict
End Function
End Module
2重复了2次
3重复了2次
4重复了3次
5重复了3次
6重复了3次