如何使用c#数组从.txt文件中删除重复项

本文关键字:删除 文件 txt 何使用 数组 | 更新日期: 2023-09-27 18:14:41

我有一个文本文件有许多重复项,我想删除重复项,然后以相同的顺序输出文件的更新版本,例如:

原来

    10
    a
    f
    a
    b
    g
    a
    f
    b
    h
    r

更新版本

    a
    f
    b
    g
    h
    r

我想使用某种数组来实现这一点这就是我目前所拥有的,

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Duplicates
{
public partial class Form1 : Form
{
    //Global Variables
    int[] Original;
    public Form1()
    {
        InitializeComponent();
    }
    //Exit Application
    private void mnuExit_Click_1(object sender, EventArgs e)
    {
        this.Close();
    }
    //Load File
    private void mnuLoad_Click_1(object sender, EventArgs e)
    {
        //code to load the numbers from a file
        OpenFileDialog fd = new OpenFileDialog();
        //open the file dialog and check if a file was selected
        if (fd.ShowDialog() == DialogResult.OK)
        {
        //open file to read
        StreamReader sr = new StreamReader(fd.OpenFile());
        int Records = int.Parse(sr.ReadLine());
        //Assign Array Sizes
        Original = new int[Records];
        //Go through text file              
        for (int i = 0; i < Records; i++)
        {
            Original[i] = int.Parse(sr.ReadLine());    
        }     
       }

    }
    private void btnOutput_Click(object sender, EventArgs e)
    {
        //store Original array
        string Output = "Original 'n";
        for (int i = 0; i < Original.Length; i++)
        {
            Output = Output + Original[i] + "'n";
        }
        int[] TempArray = new int[Original.Length];
        for (int i = 0; i < Original.Length; i++)
        {
            TempArray[i] = Original[i];
        }
    //add code here
        //output the original array and new array
        Output = Output + "Original with Delete'n";
        for (int i = 0; i < Original.Length; i++)
        {
            Output = Output + Original[i] + "'n";
        }
        lblOutput.Text = Output;
    }
}
    }

我也使用Windows窗体应用程序

如何使用c#数组从.txt文件中删除重复项

使用LINQ Distinct方法删除重复项,如下:

TempArray = Original.Distinct().ToArray();
//load and output
    private void mnuLoad_Click_1(object sender, EventArgs e)
    {
        if (fd.ShowDialog() == DialogResult.OK)
        {
            //open file to read
            StreamReader sr = new StreamReader(fd.OpenFile());
        //skip first line;
            sr.ReadLine();
            string s;
        //Remove duplications
            Dictionary<string, int> unique_lines = new Dictionary<string, int>();
            for (int i = 0; 
                (s = sr.ReadLine()) != null; //read until reach end of file
                i++)
                if(!unique_lines.Keys.Contains(s))
                    unique_lines[s] = i;//save order of line
        //Restore order:
            SortedDictionary<int, string> sorted_lines = new SortedDictionary<int, string>();
            foreach (string key in unique_lines.Keys)
                sorted_lines[unique_lines[key]] = key;
        //Output:
            string output = string.Empty;
            foreach (int key in sorted_lines.Keys)
                output += sorted_lines[key] + "'n";
            lblOutput.Text = output;
        }
    }