C# 从列表中删除两个以上的重复项
我正试图找出在C#中从列表中删除重复项的最佳方法。但是,我想保留两个重复的条目(两个完全相同的条目),同时删除所有其余条目。我见过很多从列表中删除所有重复项的例子,但我的具体问题似乎不太常见 这是我的大部分代码。背景是这是我所在班级的一个辩论计划项目。我研究了几种删除重复项的方法,在这里发现了许多方法,但不符合我的规定。每个团队每天只能辩论两次,不能超过两次,他们被随机分配到三个辩论时间,导致我的问题是,他们每天被分配超过两次 团队是随机分配的,然后移动到一个列表中,所以说重复,我的意思是我试图删除前两个重复之后的所有重复,如果它们存在的话 谢谢你,很抱歉我说的模棱两可C# 从列表中删除两个以上的重复项,c#,list,duplicates,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();
}
}
}
project3
{
公共部分类辩论时间表格式:表单
{
//球队名称有10个位置
//这些名称只能由超级管理员编辑
静态int ADMIN=1;
静态int-GUEST=2;
公共列表名称列表=新列表();
public List winList=new List();
public List lossList=新列表();
公共列表日期列表=新列表();
公共列表辩论列表=新列表();
public List week1List=新列表();
public List week2List=新列表();
public List week3List=新列表();
public List week4List=新列表();
public List week5List=新列表();
public List week6List=新列表();
public List week7List=新列表();
public List week8List=新列表();
public List week9List=新列表();
public List week10List=新列表();
公开辩论时间表格式(int x)
{
初始化组件();
initNames();
initWin();
initLoss();
起始日期();
初始辩论();
initWeekLists();
ListNames();
ListWin();
ListLoss();
ListDates();
如果(x==管理员| | x==客人)
{
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;
}
}
公开无效洗牌()
{
Random gen=新的Random();
int n=debateList.Count();
而(n>1)
{
n--;
int k=下一代(n+1);
字符串值=禁止列表[k];
辩论家[k]=辩论家[n];
辩论家[n]=价值;
}
}
公共无效列表名()
{
this.Name1.Text=nameList[0];
this.Name2.Text=名称列表[1];
this.Name3.Text=名称列表[2];
this.Name4.Text=名称列表[3];
this.Name5.Text=名称列表[4];
this.Name6.Text=名称列表[5];
this.Name7.Text=名称列表[6];
this.Name8.Text=名称列表[7];
this.Name9.Text=名称列表[8];
this.Name10.Text=名称列表[9];
}
公营机构
{
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];
}
公帑损失(
{
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];
}
公开作废日期()
{
this.Date1.Text=日期列表[0];
this.Date2.Text=日期列表[1];
this.Date3.Text=日期列表[2];
this.Date4.Text=日期列表[3];
this.Date5.Text=日期列表[4];
this.Date6.Text=日期列表[5];
this.Date7.Text=日期列表[6];
this.Date8.Text=日期列表[7];
this.Date9.Text=日期列表[8];
this.Date10.Text=日期列表[9];
}
public void initNames()
{
StreamReader sr=新的StreamReader(“nameList.txt”);
对于(int i=0;i<10;i++)
{
字符串行=sr.ReadLine();
名称列表。添加(行);
}
高级关闭();
}
public void initWin()
{
StreamReader sw=新的StreamReader(“Win.txt”);
对于(int i=0;i<10;i++)
{
字符串行=sw.ReadLine();
添加(行);
}
sw.Close();
}
公共损失()
{
StreamReader sw=新的StreamReader(“Loss.txt”);
对于(int i=0;i<10;i++)
{
字符串行=sw.ReadLine();
lossList.Add(行);
}
public List<string> debateList = new List<string>();
List<string> result = debateList.GroupBy( x => x )
.SelectMany( x => x.Take( 2 ) )
.OrderBy( x => x )
.ToList()