Showing posts with label most occured. Show all posts
Showing posts with label most occured. Show all posts

Monday, April 20, 2009

Find Longest Repeated and Most Occured string part in comma seperated values[CSV]

        private string findRepeat(string strfilenames)
        {
            string[] strfiles = Regex.Split(strfilenames, ",");
            string keystr = "";
            DataTable dt = new DataTable("StrList");
            DataColumn dcCount = new DataColumn("count", System.Type.GetType("System.Int32"));
            DataColumn dcKey = new DataColumn("keystr");
            dt.Columns.Add(dcCount);
            dt.Columns.Add(dcKey);
            foreach (string file in strfiles)
            {
                keystr = file;
                for (int i = 0; i <>
                {
                    if (keystr.Length == 0) break;
                    Regex exp = new Regex(keystr + "+",RegexOptions.IgnoreCase);
                    Match mymatch = exp.Match(strfilenames);
                    MatchCollection mycoll = exp.Matches(strfilenames);
                    if (mycoll.Count > 1)
                    {
                        DataRow dr;
                        dr=dt.NewRow();
                        dr["count"] =(mycoll.Count + keystr.Length);
                        dr["keystr"] = keystr;
                        dt.Rows.Add(dr);
                    }
                    keystr = keystr.Substring(0, keystr.Length - 1);
                }
            }
            DataRow[] drs = dt.Select("count > 1", "count DESC");
            if (drs.Length>1)
            {
                return drs[0]["keystr"].ToString();
            }
            return "";
        }