Posts

Insert an '_' before each capital letter and convert it into lowercase ()

 class Program {     static void Main()     {         // Input string         string input = "dbConnectionString";         string output = "db_connection_string";                 StringBuilder stringBuilder = new StringBuilder();          for (int i = 0; i < input.Length; i++)         {             char c = input[i];             if (char.IsUpper(c))             {                 stringBuilder.Append("_");                 stringBuilder.Append(c.ToString().ToLower());             }             else             {             ...

Input = INDIA, count each char in input string

class Program {     static void Main()     {         // Input string         string input = "INDIA";         // Call the method to count occurrences         var charCount = new Dictionary<char, int>();         // Iterate through each character in the string         foreach (var c in input)         {             // Check if the character is already in the dictionary             if (charCount.ContainsKey(c))             {                 // Increment the count of the character                 charCount[c]++;             }             else             {     ...

Find duplicate entry in array | Simple way

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 {     internal class Program     {         static void Main(string[] args)         {             int[] inputArr =new int[] { 1, 2, 3,4,1,4 };             int[] outputArr = new int[inputArr.Length];             for (int i = 0; i < inputArr.Length; i++)             {                 Array.Sort(inputArr);                 if(i != 0)                 {                     if (outputArr[i-1] != inputArr[i])                     {                 ...

Convert "reverse_me_hello" string to reverseMeHello

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 {     internal class Program     {         static void Main(string[] args)         {             string dummSytring = "reverse_me_hello";             Console.WriteLine(dummSytring);                           //Array.Reverse(stringArr);             string[] RevStringArr = dummSytring.Split('_');                          StringBuilder stringBuilder = new StringBuilder();             for (int j = 0; j < RevStringArr.Length; j++)             {                 string abc = RevStringArr[j];     ...

Reverse string in C#

Solution 1: // Include namespace system using System ; using System.Collections.Generic ; public class GFG { // Function to return the union of two arrays string dummSytring= "ReverseMe"; Console.WriteLine(dummSytring); char[] stringArr = dummSytring.ToCharArray(); Array.Reverse(stringArr); Console.WriteLine("Reverse String: "+ new string(stringArr) ); Console.ReadKey(); }

Union and Intersection of two sorted arrays in C#

Solution 1: // Include namespace system using System ; using System.Collections.Generic ; public class GFG { // Function to return the union of two arrays internal class Program { static void Main(string[] args) { int[] arr1 = { 1, 3, 4, 5, 7 }; int[] arr2 = { 2, 3, 5, 6 }; int length1 = arr1.Length; int length2 = arr2.Length; Console.WriteLine("Union"); for(int i = 0; i < arr1.Length; i++) { for(int j = 0; j < arr2.Length; j++) { if(arr1[i] == arr2[j]) { Console.WriteLine(arr1[i]); } } } int[] primaryArr = length1>length2?arr1:arr2; int[] secArr = length1<length2? arr1 : arr2; Console.WriteLine("Intersect"); for (int i = 0; i < primaryArr.Length; i++) { bool isTaken = true; for (int j = 0; j < s...

Find duplicates in an array in C#

Solution 1: // Rearrange array in alternating positive // & negative items with O(1) extra space using System ; namespace ConsoleApp1 { internal class Program { static void Main(string[] args) { int[] arr = { 1, 5, 7, -1, 5 }; List<int> list = new List<int>(); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("Duplicate values: "); for (int i = 0; i < arr.Length; i++) { int duplicay = arr.Count(x => x == arr[i]); if (duplicay > 1) { if (list.Count(x => x == arr[i]) < 1) { list.Add(arr[i]); stringBuilder.Append(arr[i]+","); } } } Console.WriteLine(stringBuilder.ToString()); Console.ReadKey(); ...