简体   繁体   中英

How to Delete Duplicates From .txt file Using Array C#

I have a text file with many duplicates and want to delete the duplicates and then output the updated version of the file in the same order, example:

Original

    10
    a
    f
    a
    b
    g
    a
    f
    b
    h
    r

Updated Version

    a
    f
    b
    g
    h
    r

I want to achieve this using an array of some sort and this is what I have so far,

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;
    }

}
    }

Also I am using Windows Forms Application

使用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;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM