我有一个名单:
int list = { 1,1,2,3,4,4,5,7,7,7,10};
现在我需要制定计算两倍数的方案。 这一数字是原来的两倍。 我希望大家理解。 因此,1个是两倍,4个是两倍,7 7个是两倍。
我有一个名单:
int list = { 1,1,2,3,4,4,5,7,7,7,10};
现在我需要制定计算两倍数的方案。 这一数字是原来的两倍。 我希望大家理解。 因此,1个是两倍,4个是两倍,7 7个是两倍。
准则中的解决办法如下:
var doubles = list.Skip(1)
.Where((number, index) => list[index] == number);
这通过欺骗名单上的第一位成员,形成了另一个顺序,然后从具有相同指数和相同价值的顺序中找到内容。 该表将按行距时间运行,但仅因为一份清单提供<编码>O(1)。
这里采取的办法相对简单,只是按顺序重复,并且按顺序进行(而不仅仅是清单):
public IEnumerable<T> FindConsecutiveDuplicates<T>(this IEnumerable<T> source)
{
using (var iterator = source.GetEnumerator())
{
if (!iterator.MoveNext())
{
yield break;
}
T current = iterator.Current;
while (iterator.MoveNext())
{
if (EqualityComparer<T>.Default.Equals(current, iterator.Current))
{
yield return current;
}
current = iterator.Current;
}
}
}
这里还有一个更为简单的例子,即它只是一个准则查询系统,但它在新条款中使用了副作用:
IEnumerable<int> sequence = ...;
bool first = true;
int current = 0;
var result = sequence.Where(x => {
bool result = !first && x == current;
current = x;
first = false;
return result;
});
第三个替代方案比较清洁,但使用<代码>。 选择性执行条码>方法,基本上为<条码>。
IEnumerable<int> sequence = ...;
IEnumerable<int> result = sequence.SelectConsecutive((x, y) => new { x, y })
.Where(z => z.x == z.y);
每个人都似乎在设法找到这样做的好办法,因此,情况确实坏:
List<int> doubles = new List<int>();
Dictionary<int, bool> seenBefore = new Dictionary<int, bool>();
foreach(int i in list)
{
try
{
seenBefore.Add(i, true);
}
catch (ArgumentException)
{
doubles.Add(i);
}
}
return doubles;
请不要这样做。
可能开展的工作包括:
list.GroupBy (l => l).Where (l => l.Count () > 1).SelectMany (l => l).Distinct();
EDIT:
以上法典没有取得被占领土希望的结果。 下面是一份经过编辑的版本,从Ani的代议解决办法中汲取灵感:
list.GroupBy(l => l).Select(g=>g.Skip(1)).SelectMany (l => l);
例如,(可能)比使用Linq做得更好,但可以说较少:
for (int i = 1; i < list.Count; i++)
if (list[i] == list[i - 1])
doubles.Add(list[i]);
这里,请见:
int[] intarray = new int[] { 1, 1, 2, 3, 4, 4, 5, 7, 7, 7, 10 };
int previousnumber = -1;
List<int> doubleDigits = new List<int>();
for (int i = 0; i < intarray.Length; i++)
{
if (previousnumber == -1) { previousnumber = intarray[i]; continue; }
if (intarray[i] == previousnumber)
{
if (!doubleDigits.Contains(intarray[i]))
{
doubleDigits.Add(intarray[i]);
//Console.WriteLine("Duplicate int found - " + intarray[i]);
continue;
}
}
else
{
previousnumber = intarray[i];
}
}
你可以这样做:
list.GroupBy(i => i).Where(g => g.Count() > 1).SelectMany(g => g.Skip(1))
这是“KJN”的答案,但我认为它表达了问题中的“两倍”和“两倍”。
g.Count() > 1
)g.Skip(1)
)www.un.org/Depts/DGACM/index_french.htm GroupBy 不要首先对清单进行分类,如果是的话,这种清单不会受到预选名单的消极影响。
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...