从列表中删除两个以上的重复项

时间:2013-11-10 18:52:44

标签: c# list duplicates

我正在尝试找出从C#中的列表中删除重复项的最佳方法。但是我希望保留两个重复的条目(两个完全相同的条目),同时删除所有其余条目。我已经看到很多从列表中删除所有重复项的示例,但我的具体问题似乎不太常见。

这是我的大部分代码。背景是这是我正在进行的一个类的辩论调度项目。我研究了一些删除重复项的方法,并在这里发现了许多但不符合我的规定。每个团队每天只能进行两次辩论而不会再进行辩论,他们会被随机分配到辩论时间,导致我的问题是每个预定日期分配的次数超过两次。

团队被随机分配,然后移动到一个列表,所以通过重复,我的意思是我试图在最初的两个之后删除所有欺骗,如果它们存在的话。

感谢和抱歉的含糊不清。

namespace SWEProject3
{
public partial class DebateSchedulerForm : Form
{
    //There are 10 slots for team names
    //The names can only be edited by the superadmin

    static int ADMIN = 1;
    static int GUEST = 2;

    public List<string> nameList = new List<string>();
    public List<string> winList = new List<string>();
    public List<string> lossList = new List<string>();
    public List<string> dateList = new List<string>();
    public List<string> debateList = new List<string>();

    public List<string> week1List = new List<string>();
    public List<string> week2List = new List<string>();
    public List<string> week3List = new List<string>();
    public List<string> week4List = new List<string>();
    public List<string> week5List = new List<string>();
    public List<string> week6List = new List<string>();
    public List<string> week7List = new List<string>();
    public List<string> week8List = new List<string>();
    public List<string> week9List = new List<string>();
    public List<string> week10List = new List<string>();

    public DebateSchedulerForm(int x)
    {
        InitializeComponent();
        initNames();
        initWin();
        initLoss();
        initDates();
        initDebates();
        initWeekLists();
        ListNames();
        ListWin();
        ListLoss();
        ListDates();


        if (x == ADMIN || x == GUEST)
        {
            Name1.ReadOnly = true;
            Name2.ReadOnly = true;
            Name3.ReadOnly = true;
            Name4.ReadOnly = true;
            Name5.ReadOnly = true;
            Name6.ReadOnly = true;
            Name7.ReadOnly = true;
            Name8.ReadOnly = true;
            Name9.ReadOnly = true;
            Name10.ReadOnly = true;

            Win1.ReadOnly = true;
            Win2.ReadOnly = true;
            Win3.ReadOnly = true;
            Win4.ReadOnly = true;
            Win5.ReadOnly = true;
            Win6.ReadOnly = true;
            Win7.ReadOnly = true;
            Win8.ReadOnly = true;
            Win9.ReadOnly = true;
            Win10.ReadOnly = true;

            Loss1.ReadOnly = true;
            Loss2.ReadOnly = true;
            Loss3.ReadOnly = true;
            Loss4.ReadOnly = true;
            Loss5.ReadOnly = true;
            Loss6.ReadOnly = true;
            Loss7.ReadOnly = true;
            Loss8.ReadOnly = true;
            Loss9.ReadOnly = true;
            Loss10.ReadOnly = true;

            ChangeDates.Visible = false;
            Save.Visible = false;
        }

    }
    public void Shuffle()
    {
        Random gen = new Random();
        int n = debateList.Count();
        while (n > 1)
        {
            n--;
            int k = gen.Next(n + 1);
            string value = debateList[k];
            debateList[k] = debateList[n];
            debateList[n] = value;
        }
    }
    public void ListNames()
    {
        this.Name1.Text = nameList[0];
        this.Name2.Text = nameList[1];
        this.Name3.Text = nameList[2];
        this.Name4.Text = nameList[3];
        this.Name5.Text = nameList[4];
        this.Name6.Text = nameList[5];
        this.Name7.Text = nameList[6];
        this.Name8.Text = nameList[7];
        this.Name9.Text = nameList[8];
        this.Name10.Text = nameList[9];
    }
    public void ListWin()
    {
        this.Win1.Text = winList[0];
        this.Win2.Text = winList[1];
        this.Win3.Text = winList[2];
        this.Win4.Text = winList[3];
        this.Win5.Text = winList[4];
        this.Win6.Text = winList[5];
        this.Win7.Text = winList[6];
        this.Win8.Text = winList[7];
        this.Win9.Text = winList[8];
        this.Win10.Text = winList[9];
    }
    public void ListLoss()
    {
        this.Loss1.Text = lossList[0];
        this.Loss2.Text = lossList[1];
        this.Loss3.Text = lossList[2];
        this.Loss4.Text = lossList[3];
        this.Loss5.Text = lossList[4];
        this.Loss6.Text = lossList[5];
        this.Loss7.Text = lossList[6];
        this.Loss8.Text = lossList[7];
        this.Loss9.Text = lossList[8];
        this.Loss10.Text = lossList[9];
    }
    public void ListDates()
    {
        this.Date1.Text = dateList[0];
        this.Date2.Text = dateList[1];
        this.Date3.Text = dateList[2];
        this.Date4.Text = dateList[3];
        this.Date5.Text = dateList[4];
        this.Date6.Text = dateList[5];
        this.Date7.Text = dateList[6];
        this.Date8.Text = dateList[7];
        this.Date9.Text = dateList[8];
        this.Date10.Text = dateList[9];
    }
    public void initNames()
    {
        StreamReader sr = new StreamReader("nameList.txt");
        for (int i = 0; i < 10; i++)
        {
            string line = sr.ReadLine();
            nameList.Add(line);
        }
        sr.Close();
    }
    public void initWin()
    {
        StreamReader sw = new StreamReader("Win.txt");
        for (int i = 0; i < 10; i++)
        {
            string line = sw.ReadLine();
            winList.Add(line);
        }
        sw.Close();
    }
    public void initLoss()
    {
        StreamReader sw = new StreamReader("Loss.txt");
        for (int i = 0; i < 10; i++)
        {
            string line = sw.ReadLine();
            lossList.Add(line);
        }
        sw.Close();
    }
    public void initDates()
    {
        StreamReader sw = new StreamReader("Dates.txt");
        for (int i = 0; i < 10; i++)
        {
            string line = sw.ReadLine();
            dateList.Add(line);
        }
        sw.Close();
    }
    public void initDebates()
    {
        StreamReader sw = new StreamReader("Debate.txt");
        for (int i = 0; i < 45; i++)
        {
            string line = sw.ReadLine();
            debateList.Add(line);
        }
        sw.Close();
    }
    public void initWeekLists()
    {

        week1List.Add(debateList[0]);
        week1List.Add(debateList[1]);
        week1List.Add(debateList[2]);
        week1List.Add(debateList[3]);
        week1List.Add(debateList[4]);

        week2List.Add(debateList[5]);
        week2List.Add(debateList[6]);
        week2List.Add(debateList[7]);
        week2List.Add(debateList[8]);
        week2List.Add(debateList[9]);

        week3List.Add(debateList[10]);
        week3List.Add(debateList[11]);
        week3List.Add(debateList[12]);
        week3List.Add(debateList[13]);
        week3List.Add(debateList[14]);

        week4List.Add(debateList[15]);
        week4List.Add(debateList[16]);
        week4List.Add(debateList[17]);
        week4List.Add(debateList[18]);
        week4List.Add(debateList[19]);

        week5List.Add(debateList[20]);
        week5List.Add(debateList[21]);
        week5List.Add(debateList[22]);
        week5List.Add(debateList[23]);
        week5List.Add(debateList[24]);

        week6List.Add(debateList[25]);
        week6List.Add(debateList[26]);
        week6List.Add(debateList[27]);
        week6List.Add(debateList[28]);

        week7List.Add(debateList[29]);
        week7List.Add(debateList[30]);
        week7List.Add(debateList[31]);
        week7List.Add(debateList[32]);

        week8List.Add(debateList[33]);
        week8List.Add(debateList[34]);
        week8List.Add(debateList[35]);
        week8List.Add(debateList[36]);

        week9List.Add(debateList[37]);
        week9List.Add(debateList[38]);
        week9List.Add(debateList[39]);
        week9List.Add(debateList[40]);

        week10List.Add(debateList[41]);
        week10List.Add(debateList[42]);
        week10List.Add(debateList[43]);
        week10List.Add(debateList[44]);

    }
    public void finNames()
    {
        StreamWriter sw = new StreamWriter("nameList.txt");
        sw.WriteLine(this.Name1.Text);
        sw.WriteLine(this.Name2.Text);
        sw.WriteLine(this.Name3.Text);
        sw.WriteLine(this.Name4.Text);
        sw.WriteLine(this.Name5.Text);
        sw.WriteLine(this.Name6.Text);
        sw.WriteLine(this.Name7.Text);
        sw.WriteLine(this.Name8.Text);
        sw.WriteLine(this.Name9.Text);
        sw.WriteLine(this.Name10.Text);
        sw.Close();
    }
    public void finWin()
    {
        StreamWriter sw = new StreamWriter("Win.txt");
        sw.WriteLine(this.Win1.Text);
        sw.WriteLine(this.Win2.Text);
        sw.WriteLine(this.Win3.Text);
        sw.WriteLine(this.Win4.Text);
        sw.WriteLine(this.Win5.Text);
        sw.WriteLine(this.Win6.Text);
        sw.WriteLine(this.Win7.Text);
        sw.WriteLine(this.Win8.Text);
        sw.WriteLine(this.Win9.Text);
        sw.WriteLine(this.Win10.Text);
        sw.Close();
    }
    public void finLoss()
    {
        StreamWriter sw = new StreamWriter("Loss.txt");
        sw.WriteLine(this.Loss1.Text);
        sw.WriteLine(this.Loss2.Text);
        sw.WriteLine(this.Loss3.Text);
        sw.WriteLine(this.Loss4.Text);
        sw.WriteLine(this.Loss5.Text);
        sw.WriteLine(this.Loss6.Text);
        sw.WriteLine(this.Loss7.Text);
        sw.WriteLine(this.Loss8.Text);
        sw.WriteLine(this.Loss9.Text);
        sw.WriteLine(this.Loss10.Text);
        sw.Close();
    }
    public void finDebates()
    {
        StreamWriter sw = new StreamWriter("Debate.txt");
        for (int i = 0; i < 45; i++)
        {
            sw.WriteLine(debateList[i]);
        }
        sw.Close();
    }
    private void Save_Click(object sender, EventArgs e)
    {
        finNames();
        finWin();
        finLoss();
        finDebates();
    }

    private void Close_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void ChangeDates_Click(object sender, EventArgs e)
    {
        ChangeDates form = new ChangeDates(dateList);
        form.ShowDialog();
        initDates();
        ListDates();
    }

    private void Date1_Click(object sender, EventArgs e)
    {
        Week_1 form = new Week_1(nameList, week1List);
        form.ShowDialog();
    }

    private void Date2_Click(object sender, EventArgs e)
    {
        Week_2 form = new Week_2(nameList, week2List);
        form.ShowDialog();
    }

}
}

2 个答案:

答案 0 :(得分:1)

您可以设置一个计数变量,遍历容器并在每次容器中存在元素时递增计数器。

答案 1 :(得分:1)

假设debateList是您要过滤的列表:

public List<string> debateList = new List<string>();

您可以使用与GroupBy方法链接的Linq SelectMany方法,并利用Take方法,即使存在较少的方法,也会占用最大值:

List<string> result = debateList.GroupBy( x => x )
                                .SelectMany( x => x.Take( 2 ) )
                                .OrderBy( x => x )
                                .ToList()