如何合并列表中的多个连续值?

时间:2019-04-05 09:29:25

标签: c# list distinct

是否具有删除后续值(即implementation 'com.android.support:support-media-compat:28.0.0' implementation 'com.android.support:support-v4:28.0.0' 14, 14 -> 14)的功能?

以下列表(12, 12 -> 12):

[12, 14, 14, 12, 12, 14]

到列表List<string> foo = new List<string> { 12, 14, 14, 12, 12, 14 };

3 个答案:

答案 0 :(得分:5)

使用foreach

public static IEnumerable<T> DistinctByPrevious<T>(List<T> source)
{
    if (source != null && source.Any())
    {
        T prev = source.First();
        yield return prev;
        foreach (T item in source.Skip(1))
        {
            if (!EqualityComparer<T>.Default.Equals(item, prev))
            {
                yield return item;
            }
            prev = item;
        }
    }
}

答案 1 :(得分:3)

Linq 没有其他库,但是具有副作用是一种快速又肮脏的prior副作用很丑)的解决方案:

  List<string> foo = new List<string> { "12", "14", "14", "12", "12", "14" };

  string prior = null;

  List<string> result = foo
    .Where((v, i) => i == 0 || v != prior)
    .Select(v => prior = v)
    .ToList();

一般情况下,您可能需要实现扩展方法

  public static partial class EnumerableExtensions {  
    public static IEnumerable<T> DistinctSuccessive<T>(
      this IEnumerable<T> source, 
           IEqualityComparer<T> comparer = null) {
      // public method arguments validation
      if (null == source)
        throw new ArgumentNullException(nameof(source));

      // equals: either default or custom one 
      Func<T, T, bool> equals = (left, right) => null == comparer 
        ? object.Equals(left, right) 
        : comparer.Equals(left, right);

      bool first = true;
      T prior = default(T);

      foreach (var item in source) {
        if (first || !equals(item, prior))
          yield return item;

        first = false;
        prior = item;
      }
    }
  }

然后

  List<string> result = foo
    .DistinctSuccessive()
    .ToList();

答案 2 :(得分:-1)

我个人更喜欢@fubo答案,但只是为了表明还有更多的变体:

var data = new[] { 12, 14, 14, 12, 12, 14 };
var result = data.Aggregate(new List<int>(), (a, e) => { if (a.FirstOrDefault() != e) a.Insert(0, e); return a; });
result.Reverse(); // we builded a list in a back order, so reverse it!