Distinct in C#

MiPakTeh 1,476 Reputation points
2022-04-02T14:10:18.517+00:00

Hi All,
I use "Distinct" to remove duplicate number from List.But my problem is when remove duplicate number the count not same as in listbox1.
listbox2 should be same with count in listbox1.

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_34
{
    public partial class Form1 : Form
    {
        List<string> Data1 = new List<string>();
        List<string> Data2 = new List<string>();
        List<string> Data3 = new List<string>();

        public Form1()
        {
            InitializeComponent();
            Data1.AddRange(File.ReadLines(@"C:\Users\family\Documents\Tool.txt"));
            //Text file = 5682,2568
            //            0251,1292
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i <= Data1.Count() - 1; i++)
            {
                var hashSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);


                listBox1.Items.Add(Data1[i]);

                var StringToCheck = Data1[i].Split(',');

                foreach (var S in StringToCheck)
                {

                    char[] arr = S.ToCharArray();
                    Console.WriteLine(arr[0].ToString());
                    string[] find = new[] {
                        arr[0].ToString() + arr[1].ToString() + arr[0].ToString(),
                        arr[0].ToString() + arr[1].ToString() + arr[1].ToString(),
                        arr[0].ToString() + arr[1].ToString() + arr[2].ToString(),
                        arr[0].ToString() + arr[1].ToString() + arr[3].ToString(),
                        arr[0].ToString() + arr[2].ToString() + arr[0].ToString(),
                        arr[0].ToString() + arr[2].ToString() + arr[1].ToString(),
                        arr[0].ToString() + arr[2].ToString() + arr[2].ToString(),
                        arr[0].ToString() + arr[2].ToString() + arr[3].ToString(),
                        arr[0].ToString() + arr[3].ToString() + arr[0].ToString(),
                        arr[0].ToString() + arr[3].ToString() + arr[1].ToString(),
                        arr[0].ToString() + arr[3].ToString() + arr[2].ToString(),
                        arr[0].ToString() + arr[3].ToString() + arr[3].ToString(),
                        arr[1].ToString() + arr[0].ToString() + arr[0].ToString(),
                        arr[1].ToString() + arr[0].ToString() + arr[1].ToString(),
                        arr[1].ToString() + arr[0].ToString() + arr[2].ToString(),
                        arr[1].ToString() + arr[0].ToString() + arr[3].ToString(),
                        arr[1].ToString() + arr[2].ToString() + arr[0].ToString(),
                        arr[1].ToString() + arr[2].ToString() + arr[1].ToString(),
                        arr[1].ToString() + arr[2].ToString() + arr[2].ToString(),
                        arr[1].ToString() + arr[2].ToString() + arr[3].ToString(),
                        arr[1].ToString() + arr[3].ToString() + arr[0].ToString(),
                        arr[1].ToString() + arr[3].ToString() + arr[1].ToString(),
                        arr[1].ToString() + arr[3].ToString() + arr[2].ToString(),
                        arr[1].ToString() + arr[3].ToString() + arr[3].ToString(),
                        arr[2].ToString() + arr[0].ToString() + arr[0].ToString(),
                        arr[2].ToString() + arr[0].ToString() + arr[1].ToString(),
                        arr[2].ToString() + arr[0].ToString() + arr[2].ToString(),
                        arr[2].ToString() + arr[0].ToString() + arr[3].ToString(),
                        arr[2].ToString() + arr[1].ToString() + arr[0].ToString(),
                        arr[2].ToString() + arr[1].ToString() + arr[1].ToString(),
                        arr[2].ToString() + arr[1].ToString() + arr[2].ToString(),
                        arr[2].ToString() + arr[1].ToString() + arr[3].ToString(),
                        arr[2].ToString() + arr[3].ToString() + arr[0].ToString(),
                        arr[2].ToString() + arr[3].ToString() + arr[1].ToString(),
                        arr[2].ToString() + arr[3].ToString() + arr[2].ToString(),
                        arr[2].ToString() + arr[3].ToString() + arr[3].ToString(),
                        arr[3].ToString() + arr[0].ToString() + arr[0].ToString(),
                        arr[3].ToString() + arr[0].ToString() + arr[1].ToString(),
                        arr[3].ToString() + arr[0].ToString() + arr[2].ToString(),
                        arr[3].ToString() + arr[0].ToString() + arr[3].ToString(),
                        arr[3].ToString() + arr[1].ToString() + arr[0].ToString(),
                        arr[3].ToString() + arr[1].ToString() + arr[1].ToString(),
                        arr[3].ToString() + arr[1].ToString() + arr[2].ToString(),
                        arr[3].ToString() + arr[1].ToString() + arr[3].ToString(),
                        arr[3].ToString() + arr[2].ToString() + arr[0].ToString(),
                        arr[3].ToString() + arr[2].ToString() + arr[1].ToString(),
                        arr[3].ToString() + arr[2].ToString() + arr[2].ToString(),
                        arr[3].ToString() + arr[2].ToString() + arr[3].ToString(),};

                    for (int j = 0; j <= find.Count() - 1; j++)
                    {
                        string AoT = find[j];
                        Data2.Add(AoT);
                    }
                }

                for (int jj = 0; jj <= Data2.Count() - 1; jj++)
                {
                    string BoT = Data2[jj];
                    int count = Convert.ToInt32(Data2.Count(s => s == BoT).ToString());
                    listBox1.Items.Add("We find the number " + "   = " + BoT + " times" + "   " + count + "   ");
                }

                Data3.AddRange(Data2);
                Data2.Clear();

                listBox2.Items.Add(Data1[i]);

                IEnumerable<string> distinctNum_ = Data3.Distinct();

                foreach (string Num in distinctNum_)
                {
                    int count = Convert.ToInt32(distinctNum_.Count(s => s == Num).ToString());
                    listBox2.Items.Add(Num + "      " + count);
                }
                Data3.Clear();

            }


        }
    }
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,535 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,476 Reputation points Microsoft Vendor
    2022-04-04T07:12:56.5+00:00

    @MiPakTeh , Welcome to Microsoft Q&A, based on your description, you want to remove duplicate number from List and keep the same as count in listbox1.

    Please change into the following code to check if it works for you.

    listBox2.Items.Add(Data1[i]);

            IEnumerable<string> distinctNum_ = Data3.Distinct();  
    
            foreach (string Num in distinctNum_)  
            {  
                int count = Convert.ToInt32(Data3.Count(s => s == Num).ToString());   //changed line  
                listBox2.Items.Add(Num + "      " + count);  
            }  
            Data3.Clear();  
    

    Note: We need to get the count from Data3 instead of istinctNum, because the distinctNum will only have one count for every number.

    Result:

    189662-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