Check the same string everylines.

MiPakTeh 1,476 Reputation points
2022-05-29T08:35:25.06+00:00

Hi All,
I have 39 lines string in List Data3 (listbox4).How to find the same string for every lines.
In button4 ,I try to use loop method and result must separate the same string and none.

Thank.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestFor_43
{
    public partial class Form1 : Form
    {
        List<string> Data1 = new List<string>();
        List<string> Data2 = new List<string>();
        List<string> Data3 = new List<string>();
        List<string> Data4 = new List<string>();

        public Form1()
        {
            InitializeComponent();
                foreach (var s in File.ReadLines(@"C:\Users\family\Documents\Test_1.txt"))
                {
                    Data1.Add(s);
                }

            for (int i = 0; i <= Data1.Count - 1; i++)
            {
                listBox1.Items.Add(i.ToString() + "     " + Data1[i]);
            }
            // Test_1.txt=
//6131,6433,3457,6615,4102,5786,4429,0554,1096,6749,6164,7932,2546,9557,7782,4423,2767,8839,2988,9450,9872,2220,2930
//8229,6309,8758,7699,1532,6548,3209,7172,6031,3161,1367,3964,9031,0645,7415,7480,1806,7664,5697,7189,4700,9372,9504
//0558,2100,6498,3658,3031,4536,0368,4647,0943,3576,2487,3854,7633,5901,8171,5300,8315,0073,9409,7738,6955,2172,0490
//6733,8571,0977,1758,2288,1280,8427,6100,7091,1285,8588,3928,2179,1486,6647,0171,1149,6691,3499,2316,8876,6179,8647
//6138,9855,8590,2506,5598,3363,3294,5097,8842,2746,2692,0462,6217,6536,4541,6274,2040,5207,9965,6093,1941,3891,1798
//6247,1084,4075,8293,3823,5994,6797,7324,6744,6758,6657,4276,4578,1496,8424,7582,8814,2551,4311,6368,9451,5157,4855
//2617,5522,4776,0297,6088,0337,7253,4374,5167,3930,1407,1118,1323,8912,5418,9738,2516,0833,6079,3447,6405,0700,0555
//3627,3096,7803,7570,8777,2863,8540,7048,1657,4994,3587,0065,6171,4839,3518,6125,9418,6703,6724,6852,7923,8295,2316
//7532,6792,6505,9696,6481,9223,1385,4187,3612,7372,5050,6716,6403,3792,4163,3504,4133,6545,5261,6284,9646,8465,3622
//2607,3836,8540,4848,8080,8018,6807,6238,2486,0771,0504,2154,9826,6737,6982,3010,8217,5414,7137,0145,2421,2362,4997
//1635,7492,6019,5295,2741,0482,7006,8132,8463,9402,3078,0863,7792,2474,7333,9239,7467,7016,5916,2106,8584,1789,4330
//4639,6922,6676,5271,1713,0473,0227,2671,2250,7284,7747,3505,1156,9734,0955,9792,6117,6947,9976,5670,1412,7253,9914
//8128,1211,0287,7649,4181,5522,9241,8835,7188,5081,2301,3470,7892,6198,9287,6095,5490,9125,6038,9326,1350,5679,4781
//2899,2093,5562,5768,7794,8404,3020,9378,0870,1049,1329,7613,3170,2902,3170,1083,7939,4531,8300,8089,1379,4365,5909
//0436,5107,9944,2291,8945,4647,7228,5580,9122,7006,1341,0214,8606,2032,1406,1573,4359,1270,6801,4196,7765,1499,6901
//1138,0886,2059,1298,1844,1327,9516,5899,1433,1912,0303,4733,8682,3493,7669,8406,5734,7835,5327,4605,8416,7601,0333
//7697,0582,9478,7943,7761,6370,0172,0726,9000,4013,1575,0429,0269,1776,8543,0452,9522,7734,9194,8382,6106,5781,8248
//4828,7042,4429,0093,4193,3859,9781,7814,7343,9454,6713,0423,5449,9330,2557,3014,2132,5898,5644,3719,2854,7552,6742
//6776,0884,8531,9925,1256,9574,3781,4469,1898,0059,4469,3139,8709,1508,3643,1724,8411,6241,1648,9507,9226,6376,3004
//2670,2673,4418,1524,3313,1264,9779,5546,0850,0327,1430,4367,7313,5662,5629,0267,2135,4572,2663,5114,9642,1204,0278
//3965,9688,8577,1431,3545,7533,4565,5590,3266,2476,8054,1720,2100,6593,6329,7501,8490,6693,9289,6515,8852,1539,5786
//7221,4947,3344,1482,1513,3673,9477,2417,7423,9165,1558,5317,6144,6638,9647,9002,5806,8747,1600,8234,1845,9410,5378
//8138,3429,1581,3960,0516,5406,3884,9944,1526,4977,2648,3222,8410,8482,0331,4759,1523,8541,4738,0836,9833,1731,8808
//0086,9763,3395,8542,5244,9760,5083,5394,8548,3578,1137,2398,0445,1043,3128,5478,9260,5566,6678,4723,8000,3594,7615
//9968,2356,1590,6003,9644,1330,3516,9920,1684,2419,3025,0763,7954,8300,3398,5947,5633,1510,9577,5387,0841,5321,5879
//1908,2137,5273,2437,4598,4287,3491,9891,2899,9067,7719,0709,9711,3701,6557,7513,5465,4189,7323,9490,5414,3137,8423
//9922,2121,1519,0073,7644,1213,9746,8655,4045,2326,0781,1909,9133,3431,7582,3739,2697,0537,4585,0244,7942,3114,2445
//7526,8322,7585,7174,3071,8155,5125,4246,6406,3412,9511,8304,5608,2849,7675,8819,9117,6481,6711,0928,2605,5574,8398
//7350,8200,9549,6836,0613,8406,5175,5913,9989,2985,7860,0858,0679,1020,4637,4230,9760,7967,5156,7142,3661,4562,7700
//0374,7860,2663,1708,6617,4432,6697,4799,6102,7775,6435,1082,8146,3488,9108,2034,4905,7574,0226,3136,7589,1886,0475
//9726,4505,8135,3561,3804,1073,0601,0173,5777,2539,3902,9497,9186,8228,9756,3182,3887,7237,7481,1509,2677,3806,2510
//7628,9297,4891,7549,6743,0759,2603,7758,3976,7697,5095,4156,7667,5139,9858,4844,1816,6390,2460,5759,8352,1282,5184
//5600,9120,6891,3544,2239,1036,0340,9984,2500,5344,9212,1242,9342,4553,9058,6747,5815,5072,5236,6447,3826,4577,8111
//7232,7812,2480,0216,3233,3031,2370,3941,7554,4102,5284,6255,3024,0473,6960,3447,1816,5243,6418,2429,5844,8509,7800
//5171,7294,5832,7261,9842,9739,1209,1467,8757,2391,5810,1316,5227,2958,9928,3631,3655,8645,4738,9864,6908,1170,1850
//8689,1251,5802,2985,5267,9712,0868,7807,9718,9000,6550,6536,6926,8262,4127,0864,8348,4337,9253,2972,3754,9519,1224
//8182,2114,9871,3335,5653,1812,5503,5310,9234,8864,0841,6947,0275,5765,0869,5877,0160,5120,0107,3023,8970,6360,4241
//0251,1292,8111,7166,6149,5676,6301,5207,3242,2205,0765,9177,9988,4080,6972,5337,9173,4805,4720,8152,8805,5028,8625
//5685,8977,6755,1198,6196,5904,3310,4159,7938,4458,0394,7348,5459,9165,0346,6047,7171,0715,0733,3528,8751,4026,4066
//9958,7361,2240,6848,2874,5092,0199,9898,9387,7323,8021,4130,4954,1651,6877,4377,0761,7249,1547,3562,0587,7777,3259


        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (var item_ in Data1)
            {
                listBox2.Items.Add(item_);

                var MyItems = new List<myItem>();

                for (int j = 0; j <= 9; j++)
                {
                    String Number_ = j.ToString();
                    int NumberCount_ = item_.Replace(",", "").ToCharArray().Count(c => c.ToString() == Number_);
                    MyItems.Add(new myItem() { Index = Convert.ToInt32(Number_.ToString()), Value = NumberCount_ });
                }

                foreach (myItem ITM in MyItems)
                {
                    listBox2.Items.Add(ITM.Index + "   = " + ITM.Value);
                }
                var Result = new Dictionary<string, int>();
                foreach (myItem ITM in MyItems)
                {
                    if (Result.ContainsKey(ITM.Value.ToString()))
                    {
                        Result[ITM.Value.ToString()] += 1;
                    }
                    else
                    {
                        Result.Add(ITM.Value.ToString(), 1);
                    }
                }
                string List = "";
                foreach (KeyValuePair<string, int> KV in Result)
                {
                    List += KV.Key + "=" + KV.Value.ToString();
                    List += ", ";
                    Data2.Add(KV.Value.ToString());
                }

                listBox2.Items.Add(" ");
                listBox2.Items.Add(List);
                listBox2.Items.Add("==========================================");

                string XList = "";

                for (int ii = 0; ii <= Data2.Count - 1; ii++)
                {
                    XList += Data2[ii];
                }
                listBox3.Items.Add(XList);

                Data3.Add(XList);

                Data2.Clear();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            for (int ii = 0; ii <= Data3.Count - 1; ii++)
            {
                string YList = Data3[ii];
                listBox4.Items.Add(ii.ToString() + "  " + YList);
                Data4.Add(YList);
            }
            Data3.Clear();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            for (int II = 0, loopTo = Data4.Count - 2; II <= loopTo; II++)
            {
                for (int JJ = II + 1, loopTo1 = Data4.Count - 1; JJ <= loopTo1; JJ++)
                {
                    if (Data4[II] == Data4[JJ])
                    {
                        listBox5.Items.Add(II.ToString() + "  " + Data4[II]);
                    }
                    else
                    {
                        listBox6.Items.Add(Data4[II]);
                    }
                }
            }
            Data4.Clear();
        }
        private void button4_Click(object sender, EventArgs e)
        {

        }


    }
}
public partial class myItem
{
    public int Index;
    public int Value;

    public override string ToString()
    {
        return Index.ToString() + " => " + Value.ToString();
    }
}
Developer technologies | C#
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2022-05-31T01:58:37.48+00:00

    @MiPakTeh , thanks for the feedback, you could try the following code to get the same item for every line in the listbox5.

    private void button3_Click(object sender, EventArgs e)  
            {  
                var duplicates = Data4  
      .Select((t, i) => new { Index = i, Text = t })  
      .GroupBy(g => g.Text)  
      .Where(g => g.Count() > 1).ToList();  
                foreach (var item in duplicates)  
                {  
                    var result = item.Select(i => i.Index + "= >" + i.Text).ToArray();  
                    string text = string.Join(" ", result);  
                    listBox5.Items.Add(text);  
                }  
                //Data4.Clear();  
            }  
    

    Result:

    206768-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.