c#logic从字符串中获取第一个非重复(不同)字符

Nov*_*Net 5 c#

在c#中我想创建逻辑,如果像abcabda这样的字符串被传递给一个方法那么它应该从字符串中返回第一个非重复字符,就像在上面它应该返回c一样.我无法将字符串转换为字符数组,然后如何将每个数组字符与字符串进行比较并返回第一个非重复字符.

我可以这样做吗?

class A
{
    static void main()
    {
        A a=new A();
        char  ch=a.m1(abcabd);
    }
}

class B
{
    char m1(string s)
    {
        string s1=s;
        char[] ch1=new char[s.length];
        for(int x=0; x<s.length;x++)
        {
            ch1[x]=s[x];
        }
        for(int x=0; x<s.length; x++)
        {
            for(int y=0; y<s.lenth; y++)
            {
                if(s[x]=ch1[y])
                {             
/// here i am confused how to create logic for comparison please let me know
// and how to return the character
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Che*_*hen 20

看来你正在寻找字符串中的第一个字符,其计数等于1?

using System.Linq;
string str = "abcabda";
char result = str.FirstOrDefault(ch => str.IndexOf(ch) == str.LastIndexOf(ch));
Run Code Online (Sandbox Code Playgroud)

非LINQ版本:

    for (int index = 0; index < str.Length; index++)
    {
        if (str.LastIndexOf(str[index]) == str.IndexOf(str[index]))
        {
            result = str[index];
            break;
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • @Danny Chen:我认为这意味着在没有吸引大脑的情况下解决这个问题,因为他似乎在做...... (6认同)

TK.*_*TK. 5

如何使用LINQ?

string test = "abcabda";

var selectedChars = (from c in test.ToCharArray() 
                    group c by c into groups
                    where groups.Count() == 1
                    select groups).First();
Run Code Online (Sandbox Code Playgroud)

这将根据问题中给出的示例返回"c".


And*_*lon 5

很简单但是:

    private char? findNonRepeat(string search)
    {
        foreach(char chr in search)
        {
            if (search.LastIndexOf(chr) == search.IndexOf(chr))
                return chr;
        }

        return null;
    }
Run Code Online (Sandbox Code Playgroud)


Fre*_*örk 5

您可以使用一点LINQ:

char result = input
    .GroupBy(c => c)             // group the characters
    .Where(g => g.Count() == 1)  // filter out groups with only 1 occurence
    .Select(g => g.Key)          // get the character from each remaining group
    .First();                    // return the first one
Run Code Online (Sandbox Code Playgroud)


Gab*_*abe 3

这是到目前为止您的代码:

char m1(string s) 
{ 
    string s1=s; 
    char[] ch1=new char[s.length]; 
    for(int x=0; x<s.length;x++) 
    { 
        ch1[x]=s[x]; 
    } 
    for(int x=0; x<s.length; x++) 
    { 
        for(int y=0; y<s.lenth; y++) 
        { 
            if(s[x]=ch1[y]) 
            {              
/// here i am confused how to create logic for comparison please let me know 
// and how to return the character 
            } 
        } 
    } 
} 
Run Code Online (Sandbox Code Playgroud)

你实际上很接近。我将忽略所有的风格问题和冗余,因为它们不是你要问的。

您真正想做的是逐个字符地遍历字符串,并查看该字符稍后是否存在于字符串中。如果该角色重复出现,您可以停止寻找该角色并继续寻找下一个角色。如果到达字符串末尾而没有找到重复字符,则说明您找到了非重复字符并且可以将其返回。您的嵌套 x/y 循环中拥有大部分逻辑,但缺少一些内容:

    for(int x=0; x<s.length; x++) 
    {
        // you need a flag here to indicate whether your character is a duplicate
        for(int y=0; y<s.lenth; y++) // this loop should start at x+1, not 0
        { 
            if(s[x]=ch1[y]) // you need == instead of =
            {              
                // if you've gotten here, you have a duplicate --
                // set your flag to true and break out of the loop
            }
        }
        // at this point you should check your flag and
        // if it's not set, return your character: s[x]
    }
Run Code Online (Sandbox Code Playgroud)

编辑:您提到您希望能够在不调用的情况下找到字符串的长度Length,并且大概不需要命名任何其他方法。执行此操作的方法是使用循环foreach来迭代字符串中的字符,并在执行过程中递增计数器。以下是一些带有注释的代码供您填写:

// initialize counter to 0
foreach (char ch in s)
{
    // increment counter by 1
}
// now counter is the length of the string
Run Code Online (Sandbox Code Playgroud)