简体   繁体   中英

Delete Duplicates From Text File Using Array

This is my code but I'm not sure what to put in a certain area (see below) as I keep getting an error there. I am basically loading up a text file and then deleting any values that are repeated and then outputting the updated copy of the text file.

The text file looks like,

    5
    5
    3
    2
    2
    3

My code is

{
public partial class Form1 : Form
{
    //Global Variable
    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";

        //Output Original Array
        for (int i = 0; i < Original.Length; i++)
        {
            Output = Output + Original[i] + "\n";
        }

        //Create TempArray
        int[] TempArray = new int[Original.Length];

        //Set TempArray Equal to Original Array
        for (int i = 0; i < Original.Length; i++)
        {
            TempArray[i] = Original[i];
        }

        //Current Index
        int Counter = 0;

        //Loop Through Entire Array
        for (int i = 0; i < TempArray.Length; i++)
        {
            for (int j = i + 1; j < TempArray.Length; j++)
            {
                //Replace Duplicate Values With '-1'
                if (TempArray[i] == TempArray[j])
                {
                    TempArray[j] = -1;
                    Counter++;
                }
            }

        }

        //Set Size of Original Array
        Original = new int[Original.Length - Counter];

        //Counter = 0;
        //Remove -1 Values

//error begins here
for (int i = 0; i < Original.Length; i++)
        {
            for (int j = i + 1; j < Original.Length; j++)
            {
                //Set Original Array Equal to TempArray For Values Not Equal To '-1'
                if (j != -1)
                {
                    Original[j] = TempArray[j];
                    //Counter++;
                }
            }
        }
 //error ends here
        //Final Output -- The New Array
        Output = Output + "Original Without Duplicates\n";

        for (int i = 0; i < Original.Length; i++)
        {
            Output = Output + Original[i] + "\n";
        }
        lblOutput.Text = Output;

    }
}
}

I understand your logic, but wer'e all lazy programmers. You could simply use LINQ in order to prevent duplication. Load the array as you did already and use the Distinct method somthing like this:

int[] newArray = Orginal.Distinct().ToArray();

Goodluck.

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