根据重复元素单独列出

时间:2016-12-04 13:03:30

标签: c#

我有STAR,SUN,MOON(例子)的枚举

我有以下列表:

STAR,
SUN,
SUN,
SUN,
SUN,
SUN,
MOON,
STAR,
STAR,
STAR,
STAR,
STAR,
STAR,
STAR,
STAR,
STAR,
STAR,
STAR,
STAR,
MOON

我正在寻找一种方法来产生以下列表:

STAR,
SUN,
MOON,
STAR,
STAR,
MOON

如果我们有相同类型的1-5个元素,我们添加1个条目,如果我们有超过5个,我们添加数字/ 5或6,如果我们有15个卫星我们将3添加到新列表,如果我们有22个,我们加4,如果我们有30,我们加5等等

2 个答案:

答案 0 :(得分:0)

2个实现,选择你喜欢的那个。

首先(从原始列表中删除项目):

// usage:
MyFilter(list);

// method:
static void MyFilter(List<MyEnum> list)
{
    int repetitions = 1;
    MyEnum repeatValue = list[0];
    int i = 1;
    while(i < list.Count) {
        MyEnum current = list[i];

        if(current == repeatValue) {
            repetitions++;
            if(repetitions>9 && repetitions%5==0)
                i++;
            else
                list.RemoveAt(i);
        } else {
            repetitions=1;
            repeatValue=current;
            i++;
        }
    }
}

第二个(建立一个新的清单):

// usage:
list = MyFilter(list);

// method:
static List<MyEnum> MyFilter(List<MyEnum> list)
{
    List<MyEnum> result = new List<MyEnum>();

    int repetitions = 0;
    MyEnum? repeatValue = null;
    for(int i=0; i<=list.Count; i++) {
        MyEnum? current = null;
        if(i < list.Count) {
            current = list[i];

            if(repeatValue==null || current==repeatValue) {
                repetitions++;
                repeatValue=current;
                continue;
            }
        }

        if(repetitions <= 5) {
            result.Add(repeatValue.Value);
        } else {
            for(int j=repetitions/5; j>0; j--)
                result.Add(repeatValue.Value);
        }

        repetitions = 1;
        repeatValue = current;
    }

    return result;
}

答案 1 :(得分:-1)

你需要在找到第一项时输出值,然后跳过2到5.我用字典解决了

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
            string[] input = { "STAR",
                                "SUN",
                                "SUN",
                                "SUN",
                                "SUN",
                                "SUN",
                                "MOON",
                                "STAR",
                                "STAR",
                                "STAR",
                                "STAR",
                                "STAR",
                                "STAR",
                                "STAR",
                                "STAR",
                                "STAR",
                                "STAR",
                                "STAR",
                                "STAR",
                                "MOON"
                             };
            Dictionary<string, int> dict = new Dictionary<string, int>();
            List<string> output = new List<string>();

            foreach (string str in input)
            {
                if (dict.ContainsKey(str))
                {
                    int count = dict[str];
                    if ((count % 5) == 0)
                    {
                        output.Add(str);
                    }
                    dict[str] += 1; 
                }
                else
                {
                    dict.Add(str, 0);
                }
            }

        }

    }
}