Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,104 questions

40,777 answers

573 users

How to check whether a string contains duplicate characters in C#

2 Answers

0 votes
using System;
using System.Linq;

class Program
{
    static void Main() {
        String s = "c# program-ing"; 
       
        bool b = s.Where((ch, i) => i > 0 && ch == s[i - 1] )
                         .Cast<char?>()
                         .FirstOrDefault() != null;
                         
        Console.WriteLine(b);
    }
}




/*
run:

False

*/

 





answered Jan 11, 2020 by avibootz
0 votes
using System;

class Program
{
    static bool contains_duplicate_characters(string s) {
        if (string.IsNullOrEmpty(s)) return false;
    
        bool b = false;
        for (int i = 0 ; i < s.Length - 1 && !b; i++) {
            b = s[i] == s[i + 1];
        }
    
        return b;
    }
    static void Main() {
        String s = "c# program-ingg"; 
       
        Console.WriteLine(contains_duplicate_characters(s));
    }
}




/*
run:

True

*/

 





answered Jan 11, 2020 by avibootz
...