要求!:
比如此函数名字为:bindGrid(string [],int i)
参数string []即表示数组,i呢表示string []数组的元素相同的个数!
例如:bindGrid(string [],5);即提取这个string []数组中出现5次的元素;
请各位给个例子!
拜托!
比如此函数名字为:bindGrid(string [],int i)
参数string []即表示数组,i呢表示string []数组的元素相同的个数!
例如:bindGrid(string [],5);即提取这个string []数组中出现5次的元素;
请各位给个例子!
拜托!
或你自己写一个类似count(*) ... group by ... having count(*)=5 的方法。 static string GetRepeatedElement(string[] strs, int repeats)
{
Dictionary<string, int> dict = new Dictionary<string, int>();
foreach (string s in strs)
{
if (dict.ContainsKey(s)) dict[s]++;
else dict[s] = 1;
} foreach (string s in dict.Keys)
{
if (dict[s] == repeats) return s;
}
return null;
}
private string[] bindGrid(string[] array,int i)
{
HashTable ht = new HashTable();
foreach(string s in array)
{
if(ht.ContainsKey(s))
ht[s]=Convert.ToInt32(ht[s])+1;
else
ht.Add(s,1);
}
List<string> list=new List<string>();
IDictionaryEnumerator ide = ht.GetEnumerator();
while(ide.MoveNext())
{
if(ide.Value.ToString()==i)
list.Add(ide.Value.ToString());
}
string[] result= list.ToArray();
return result;
}
bindGrid(asa,5); private static void bindGrid(string[] n,int max)
{
int count=0;
for (int i = 0; i < n.Length;i++ )
{
for (int j = i + 1; j < n.Length;j++ )
{
if (count-1 > max)
{
Console.Write(n[i].ToString());
Console.Read();
}
else
{
if (n[i].Equals(n[j])) count++;
}
}
}
}
public string[] bindGrid(string[] str, int i)
{
int counter=0;
System.Collections.ArrayList alist = new System.Collections.ArrayList();
for (int j = 0; j < str.Length; j++)
{
for (int k = 0; k < str.Length; k++)
{
if (str[j] == str[k])
counter++;
} if (counter == i)
alist.Add(str[j]); }
return (string [])alist.ToArray(typeof(string));
}
string[] str = new string[] { "a", "b", "a", "c", "d", "e", "c", "a" ,"e","a","c","c","c","a"};
public System .Collections .ArrayList bindGrid(string[] str, int i)
{
int counter=0;
System.Collections.ArrayList alist = new System.Collections.ArrayList();
for (int j = 0; j < str.Length; j++)
{
counter = 0;
for (int k = 0; k < str.Length; k++)
{
if (str[j] == str[k])
counter++;
} if (counter == i&& alist .IndexOf (str [j])<0)//滤除相同元素
alist.Add(str[j]);
}
return alist; }
我的方法是每一个元素都和数组的每一个元素进行比较,如果出现相同的,计数器就加一,当比较完之后看计数器是否等于i,
同时一定要滤除相同元素.
测试通过,不过返回的是arraylist,具体返回什么楼主没有要求.
return (string [])alist.ToArray(typeof(string));
{
StringBuilder sb = new StringBuilder(50);
List<string> list = new List<string>();
Array.Sort(array);
foreach (string s in array)
sb.Append(s+",");
foreach (System.Text.RegularExpressions.Match match in System.Text.RegularExpressions.Regex.Matches(sb.ToString(), @"(?<repeat>\w\,)(?:\k<repeat>){" + (n-1) + "}"))
{
string element = match.ToString().Substring(0,match.ToString().IndexOf(",")+1);
if (!System.Text.RegularExpressions.Regex.IsMatch(sb.ToString(), @"("+element + "){" + (n+1) + "}"))
list.Add(element);
}
return list;
}