【问题标题】:Compress Items in Array C#Compress Items in Array C#
【发布时间】:2022-12-28 09:51:17
【问题描述】:

I have the following list:

List<int> Items = new List<int> { 0, 0, 1, 1, 0 }

I want same items in the array that are next to each other to continue to add up until the next item in the array that is not the same, with the output being the following:

0,2
1,2
0,1

This is the code I have:

    public static void Test()
    {
        StringBuilder Data = new StringBuilder();
        List<int> Items = new List<int> { 0, 0, 1, 1, 0 };
        int Index = 1;

        if (Items[0] != Items[1])
        {
            Data.AppendLine(Items[0] + " 1");
        }
        while (Index < Items.Count)
        {
            int PastValue = Items[Index - 1];
            int CurrentValue = Items[Index];
            int CurrentLength = 1;

            while (CurrentValue == PastValue && Index + CurrentLength < Items.Count)
            {
                CurrentValue = Items[Index + CurrentLength];
                CurrentLength += 1;
            }
            Data.AppendLine(CurrentValue + " " + CurrentLength);

            Index = Index + CurrentLength; 
        }
        Console.WriteLine(Data.ToString());
    }

And it produces the following which is incorrect:

1,2
0,2

Is there a better way of doing this? Any help much appreciated.

【问题讨论】:

    标签: c# arrays


    【解决方案1】:

    Let's start with this extension method:

    public static IEnumerable<(V Value, int Count)> Compress<V>(this IEnumerable<V> source)
    {
        var count = 0;
        var current = default(V);
        foreach (var value in source)
        {
            if (count == 0)
            {
                current = value;
                count = 1;
            }
            else
            {
                if (current.Equals(value))
                {
                    count++;
                }
                else
                {
                    yield return (current, count);
                    current = value;
                    count = 1;
                }
            }
        }
        if (count != 0)
        {
            yield return (current, count);
        }
    }
    

    Now I can write this:

    List<int> Items = new List<int> { 0, 0, 1, 1, 0 };
    List<(int Value, int Count)> output = Items.Compress().ToList();
    

    That gives me:

    To display that it becomes easy:

    string display = String.Join(Environment.NewLine, output.Select(o => $"{o.Value},{o.Count}"));
    Console.WriteLine(display);
    

    That gives me:

    0,2
    1,2
    0,1
    
      【解决方案2】:

      Another one:

      public static void Main(string[] args)
      {
          List<int> Items = new List<int> { 0, 0, 1, 1, 0 };
      
          int count = 0;
          int prevInt = 0;
          List<Tuple<int, int>> results = new List<Tuple<int, int>>();            
          foreach(int i in Items)
          {
              if (count == 0 || i == prevInt)
              {
                  count++;
              }
              else
              {
                  results.Add(new Tuple<int, int>(prevInt, count));
                  count = 1;
              }
              prevInt = i;
          }
          if (count > 0)
          {
              results.Add(new Tuple<int, int>(prevInt, count));
          }
      
          String result = String.Join(Environment.NewLine, results);
          Console.WriteLine(result);
          Console.WriteLine("Press Enter to Quit.");
          Console.ReadLine();
      }
      

      Output:

      (0, 2)
      (1, 2)
      (0, 1)
      Press Enter to Quit.
      
        【解决方案3】:
         public static void Test()
                {
                    StringBuilder Data = new StringBuilder();
                    List<int> Items = new List<int> { 0, 0, 1, 1, 0 };
                    int tempCount = 0;
                    int tempNumber = 0;
                    for ( int i = 0; i < Items.Count; i++ )
                    {
                        if ( Items[i].Equals(tempNumber) )
                        {
                            tempCount++;
                        }
                        else
                        {
                            if(tempCount>0)
                            {
                                Data.AppendLine( tempNumber + "," + tempCount );
                            }
                            tempNumber = Items[i];
                            tempCount = 1;
                        }
                    }
                    Data.AppendLine( tempNumber + "," + tempCount );
                    Console.WriteLine( Data.ToString() );
                }
        
        • This is awesome! Thank you so much, it works perfectly. I don't know why someone downvoted it.
        猜你喜欢
        • 1970-01-01
        • 2022-12-02
        • 2022-12-02
        • 2013-01-09
        • 2020-11-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-29
        • 1970-01-01
        相关资源
        最近更新 更多