ASP.net gets the duplicate data code in the array

Source: Internet
Author: User

ASP tutorial. NET get the duplicate data code in an array
The following provides five different ways to remove duplicate data from the data and count the number of repetitions.

dictionary<int, int> dic = new Dictionary ();
int[] arr = new int[] {1, 5, 2, 6, 7, 1, 5, 4, 1, 6, 8, 7, 6};
foreach (int i in arr)
{
Try
{
Dic.add (i, I);
}
catch (Exception err)
{
//.... ...
}
}


Method Two

int[] arr = new int[] {1, 5, 2, 6, 7, 1, 5, 4, 1, 6, 8, 7, 6};
int n = arr.length-arr.distinct (). Count ();

Method Three

int[] ary = new int[] {1, 3, 3, 4, 5, 4};
var q = from X in ary
Group x by x into Y
Select {Y.key,y.count ()};



Method Four

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};
var GS = arr.groupby (i => i). by (I => i.count ());
Console.WriteLine (Gs.lastordefault (). ToArray () [0]);

Method Five

Int[] arr = {1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5};
var linqer =
From A in arr
Group Arr by A into B
by B.count ()
Select New
{
B.key,
Count = B.count ()
};
foreach (var i in Linqer)
{
Console.WriteLine ("{0} appears {1} times", I.key, I.count);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.