Skip to content

Instantly share code, notes, and snippets.

@abbood
Created March 7, 2017 07:22
Show Gist options
  • Select an option

  • Save abbood/f1483cc323ae3b9223374682be9d335b to your computer and use it in GitHub Desktop.

Select an option

Save abbood/f1483cc323ae3b9223374682be9d335b to your computer and use it in GitHub Desktop.
//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static char FirstNonRepeatedChar(char compareChar, string sentence, int compareCharPos)
{
//checking if last char of passed sentence is reached
if (sentence.Length == compareCharPos)
{
//checking if passed char exists once in passed sentence.
if (sentence.Count(obj => obj == compareChar) == 1)
//return the last sentence char as the first none repeated char
return sentence[sentence.Length - 1];
else
//the sentence does not contains any non repeated char
return '\0';
}
//checking if passed char exists once in passed sentence.
else if (sentence.Count(obj => obj == compareChar) == 1)
{
//return the current method call compare char as the first none repeated char
return compareChar;
}
else
{
//the current method call compare char exists more than once and the primitive case of recursion is not reached yet
//So call the method recursively with compareCharPos incremented by 1
return FirstNonRepeatedChar(sentence[compareCharPos], sentence, compareCharPos + 1);
}
}
public static void Main(string[] args)
{
//Your code goes here
Console.WriteLine("Hello, world!");
Console.WriteLine(Program.FirstNonRepeatedChar('r',"total", 0));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment