int[] arr = {1,2,.....,n};var g=from a in arr group a by a into c where c.count()>1 select c 试试LINQ
Sub Main() Dim a = {1, 2, 3, 4, 5, 1, 2, 3} Dim b = From o In a Group o By o Into Count() Where Count > 1 For Each o In b Console.WriteLine(o.o) Next End Sub
string[] arr = new string[] { "12345678", "12345678", "123456789" }; //初始化Dictionary对象 SortedDictionary<string, int> freq = new SortedDictionary<string, int>(); foreach (string dicstr in arr) { if (!string.IsNullOrEmpty(dicstr.Trim())) { if (freq.ContainsKey(dicstr.Trim())) { freq[dicstr.Trim()]++; } else { freq.Add(dicstr.Trim(), 1); } } }
改变成LZ想要的int[] intArray = {1,2,3,4,5,1,2,3}; //List用于存储从数组里取出来的不相同的元素 List<int> listInt = new List<int>(); foreach (int eachInt in intArray) { if (!listInt.Contains(eachInt)) listInt.Add(eachInt); } //最后从List里取出各个数字进行操作 foreach (int eachInt in listInt) { Console.Write(eachInt); //打印每个数字 }
int [] a={1,1,3,2,4,5,4,7,8,9,4,};
List<int> list = new List<int>(); for (int i = 0; i < a.Length;i++ ) list.Add(a[i]); for (int k = 0; k < list.Count;k++ ) { if (list.Contains(a[k])) { list.Remove(a[k]); if (list.Contains(a[k])) list.Add(a[k]); } }试试
int[] arr = { 1, 2, 3, 4, 5, 1, 2, 3 }; var linq = from a in arr group a by a into g where g.Count() > 1 select g; foreach (var l in linq) Console.WriteLine(l.Key);
int[] str = {1,2,3,4,5,6,7,8,2,5,6,8 }; var s = from n in str group n by n into c where c.Count() > 1 select c;
foreach (var t in s) //遍历对象 { foreach (var tt in t) { Response.Write(tt.ToString() + "<br/>"); //输出查询结果 } }这是linq的方式,也可以用楼上的方式。
group a by a into c
where c.count()>1
select c
试试LINQ
Sub Main() Dim a = {1, 2, 3, 4, 5, 1, 2, 3} Dim b = From o In a
Group o By o Into Count()
Where Count > 1 For Each o In b
Console.WriteLine(o.o)
Next
End Sub
private void Compare()
{
string str = null;
int[] arr1 = { 1, 2, 5, 4, 7, 8, 5,1, 9 };
for (int i = 0; i < arr1.Length; i++)
{
for (int j = i+1; j < arr1.Length; j++)
{
if (arr1[i] == arr1[j])
{
str += arr1[i].ToString()+" ";
}
}
}
MessageBox.Show(str);
}
string[] arr = new string[] { "12345678", "12345678", "123456789" };
//初始化Dictionary对象
SortedDictionary<string, int> freq = new SortedDictionary<string, int>(); foreach (string dicstr in arr)
{
if (!string.IsNullOrEmpty(dicstr.Trim()))
{
if (freq.ContainsKey(dicstr.Trim()))
{
freq[dicstr.Trim()]++;
}
else
{
freq.Add(dicstr.Trim(), 1);
}
}
}
//List用于存储从数组里取出来的不相同的元素
List<int> listInt = new List<int>();
foreach (int eachInt in intArray)
{
if (!listInt.Contains(eachInt))
listInt.Add(eachInt);
}
//最后从List里取出各个数字进行操作
foreach (int eachInt in listInt)
{
Console.Write(eachInt); //打印每个数字
}
List<int> list = new List<int>();
for (int i = 0; i < a.Length;i++ )
list.Add(a[i]);
for (int k = 0; k < list.Count;k++ )
{
if (list.Contains(a[k]))
{
list.Remove(a[k]);
if (list.Contains(a[k]))
list.Add(a[k]);
}
}试试
var linq = from a in arr group a by a into g where g.Count() > 1 select g;
foreach (var l in linq)
Console.WriteLine(l.Key);
var s = from n in str
group n by n into c
where c.Count() > 1
select c;
foreach (var t in s) //遍历对象
{
foreach (var tt in t)
{
Response.Write(tt.ToString() + "<br/>"); //输出查询结果
}
}这是linq的方式,也可以用楼上的方式。