从数组中删除重复项

时间:2015-09-30 01:15:04

标签: c# arrays

我有一个代码,我加载一个文本文件并将其保存为数组。该数组包含一个数字列表,其中一些数字是重复的。在我的代码中,我首先遍历数组并用-1替换所有重复的数字。然后我打算从我的数组中删除所有-1值。然后将剩余的数组值(非重复数据)复制到要输出的新数组。

但是当我尝试从数组中删除-1值时,我一直收到错误(请参阅下面的代码)。我不知道为什么会这样,所以如果有人知道的话请告诉我!

P.S。这是学校项目,所以我只能使用循环和if语句,而不是像LINQ或foreach等。

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

        //Duplicate Number Counter
        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];

        //Remove -1 Values

        //Index Counter
        int Index = 0;
//error starts
        for (int i = 0; i < TempArray.Length; i++)
        {

            if (TempArray[i] != -1)
            {
                 Original[Index] = TempArray[i];
                Index++;
            }
        }
//error ends
        //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;

        }
    }
}

3 个答案:

答案 0 :(得分:0)

在这种情况下你唯一需要做的就是

Original = new int[Original.Length - (Counter-1)];

并且是的,您需要更改代码以将值分配给Original而不是TempArray。

//error starts
for (int i = 0; i < TempArray.Length; i++)
{

    if (TempArray[i] != -1)
    {
        Original[Index] = TempArray[i];
        Index++;
    }
}
//error ends

答案 1 :(得分:0)

将行TempArray[i] = Original[Index];反转为Original[Index] = TempArray[i];

答案 2 :(得分:0)

假设您的数组中有三个3。在迭代并遇到3的外部循环中,您将另外两个3设置为-1。现在您遇到了先前为-1的第一个3,并且您已将最后3设置为-1。现在您搜索重复项并错误地计算重复数字计数器(称为Counter)中的重复项数量,因为-1-1重复。

所以,你可以做的是:

首先只将所有重复项设置为-1,然后再次循环遍历数组,以计算-1的数量。这样,您将正确设置非重复数组元素的大小。

将所有重复设置为-1

for (int outerIndex = 0; outerIndex < Original.Length; outerIndex++)
{
    var currentElement = Original[outerIndex];
    for (int innerIndex = outerIndex + 1; innerIndex < Original.Length; innerIndex++)
    {
        if (Original[innerIndex] == currentElement) Original[innerIndex] = -1;
    }
}

现在计算非重复元素:

var counter = 0;
for (int index = 0; index < Original.Length; index++)
{
    if (Original[index] != -1) counter++;
}

创建非重复元素的数组:

var newArrayIndex = 0;
var newArray = new int[Original.Length - counter]; //Calculated counter above
for(int i = 0; i < Original.Length; i++)
{
    if (Original[i] != -1) newArray[newArrayIndex++] = Original[i];
}
//Finally, not good programming practice, but 
//you can set Original to this newArray if you like...
Original = newArray;