C #: Using Hashtable to output the information that those users publish the most topics

Source: Internet
Author: User

Idea: Calculate the number of individual, the number of the most, the output details

The specific algorithm is as follows:

     Public classCount {#regionCalculate the number of entities Public StaticHashtable Entitycount (DataTable DT,stringclmnuid) {            //list<string> lst = new list<string> ();Hashtable Uidscount =NewHashtable (); foreach(DataRow rowinchdt. Rows) {stringUID =Row[clmnuid].                ToString (); if(Uidscount.contains (UID)) {Uidscount[uid]= (int) Uidscount[uid] +1; }                Else{uidscount.add (uid,1); }            }            returnUidscount; }        #endregion        #regionGet the largest number of entities Public Staticilist<string> Maxcountuid (Hashtable uidscount,BOOLListresult) {            intMaxCount =0; IList<string> UIDs =Newlist<string>(); intValue =0; foreach(DictionaryEntry kvinchUidscount) {Value= (int) kv.                Value; if(Value >MaxCount) {MaxCount=value; UIDs.                    Clear (); UIDs. ADD (KV.                Key.tostring ()); }                Else if(Value = =MaxCount) {UIDs. ADD (KV.                Key.tostring ()); }            }            returnUIDs; }         Public StaticHashtable maxcountuid (Hashtable uidscount) {intMaxCount =0; Hashtable UIDs=NewHashtable (); intValue =0; foreach(DictionaryEntry kvinchUidscount) {Value= (int) kv.                Value; if(Value >MaxCount) {MaxCount=value; UIDs.                    Clear (); UIDs. ADD (KV. Key,kv.                Value); }                Else if(Value = =MaxCount) {UIDs. ADD (KV. Key, KV.                Value); }            }            returnUIDs; }        #endregion        #regionThe ID, name, and quantity of the output user Public Static voidPrintmaxcountname (ilist<string> uids,datatable Users,stringClmnuid,stringclmnname) {             foreach(DataRow Drinchusers. Rows) {stringStruid =Dr[clmnuid].                ToString (); foreach(stringUidinchUIDs) {                    if(UID = =struid) {                        stringStrName =Dr[clmnname].                        ToString (); Console.WriteLine (Struid+":"+strName); }                }            }        }         Public Static voidPrintmaxcountname (Hashtable UIDs, DataTable users,stringClmnuid,stringclmnname) {            foreach(DataRow Drinchusers. Rows) {stringStruid =Dr[clmnuid].                ToString (); foreach(DictionaryEntry kvinchUIDs) {                    if(KV. key.tostring () = =struid) {                        stringStrName =Dr[clmnname].                        ToString (); Console.WriteLine (Struid+":"+ StrName +"("+ KV. Value.tostring () +")"); }                }            }        }        #endregion    }
View Code

To construct the test data:

     Public classData {Private string[] Userscolumns = {"UID","name" }; Private string[] Subjectscolumns = {"title","UID"," Time" };  PublicDataTable Initusers () {DataTable users=NewDataTable (); foreach(stringColumnNameinchuserscolumns) {Users.            Columns.Add (ColumnName); }             for(inti =0; I <Ten; i++) {users. Rows.Add ("UID"+i,"name"+i); }            returnusers; }         PublicDataTable Initsujects () {DataTable Subjects=NewDataTable (); foreach(stringColumnNameinchsubjectscolumns) {subjects.            Columns.Add (ColumnName); }             for(inti =0; I <123; i++) {subjects. Rows.Add ("title"+ I,"UID"+ i%Ten," Time"+1); }            returnsubjects; }    }
View Code

Test code:

    classProgram {Static voidMain (string[] args) {            Const stringClmnuid ="UID"; Const stringClmnname ="name"; Data Data=NewData (); DataTable Users=data.initusers (); DataTable Subjects=data.initsujects (); Hashtable Uidscount=Count.entitycount (subjects, clmnuid); Hashtable Maxcountuids=Count.maxcountuid (Uidscount);            Count.printmaxcountname (Maxcountuids,users,clmnuid,clmnname);        Console.read (); }    }
View Code

Output Result:
UID0:NAME0 (13)

UID1:NAME1 (13)

Uid2:name2 (13)

C #: Using Hashtable to output the information that those users publish the most topics

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.