首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >根据重复的元素单独列出

根据重复的元素单独列出
EN

Stack Overflow用户
提问于 2016-12-04 13:03:30
回答 2查看 47关注 0票数 0

我有星辰,太阳,月亮(例如)

我有以下清单:

代码语言:javascript
运行
复制
STAR,
SUN,
SUN,
SUN,
SUN,
SUN,
MOON,
STAR,
STAR,
STAR,
STAR,
STAR,
STAR,
STAR,
STAR,
STAR,
STAR,
STAR,
STAR,
MOON

我正在寻找一种制作以下清单的方法:

代码语言:javascript
运行
复制
STAR,
SUN,
MOON,
STAR,
STAR,
MOON

如果我们有来自同一类型的1-5元素,我们添加了1项,如果我们有超过5,我们添加了数字/5或6,如果我们有15个月亮,我们在新列表中添加3,如果我们有22,我们添加4,如果我们有30,我们添加5等等。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-04 13:30:00

2个实现,选择您喜欢的一个。

第一(从原始列表中删除项目):

代码语言:javascript
运行
复制
// 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++;
        }
    }
}

第二(构建一个新列表):

代码语言:javascript
运行
复制
// 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;
}
票数 0
EN

Stack Overflow用户

发布于 2016-12-04 13:35:06

您需要在找到第一项时输出值,跳过2到5项。

代码语言:javascript
运行
复制
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);
                }
            }

        }

    }
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40958932

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档