Remove Duplicate Lines From Text File?

后端 未结 5 2226
孤街浪徒
孤街浪徒 2020-12-09 05:58

Given an input file of text lines, I want duplicate lines to be identified and removed. Please show a simple snippet of C# that accomplishes this.

5条回答
  •  悲&欢浪女
    2020-12-09 06:33

    I am new to .net & have written something more simpler,may not be very efficient.Please fill free to share your thoughts.

    class Program
    {
        static void Main(string[] args)
        {
            string[] emp_names = File.ReadAllLines("D:\\Employee Names.txt");
            List newemp1 = new List();
    
            for (int i = 0; i < emp_names.Length; i++)
            {
                newemp1.Add(emp_names[i]);  //passing data to newemp1 from emp_names
            }
    
            for (int i = 0; i < emp_names.Length; i++)
            {
                List temp = new List();
                int duplicate_count = 0;
    
                for (int j = newemp1.Count - 1; j >= 0; j--)
                {
                    if (emp_names[i] != newemp1[j])  //checking for duplicate records
                        temp.Add(newemp1[j]);
                    else
                    {
                        duplicate_count++;
                        if (duplicate_count == 1)
                            temp.Add(emp_names[i]);
                    }
                }
                newemp1 = temp;
            }
            string[] newemp = newemp1.ToArray();  //assigning into a string array
            Array.Sort(newemp);
            File.WriteAllLines("D:\\Employee Names.txt", newemp); //now writing the data to a text file
            Console.ReadLine();
        }
    }
    

提交回复
热议问题