Posts

Showing posts from June, 2024

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(); ...

Count pairs with given sum in C#

Solution 1: // C# implementation of simple // method to find count of // pairs with given sum. using System ; internal class Program { static int[] arr = new int[] { 1, 2, 3, 4, 5 }; static void Main(string[] args) { int sum = Convert.ToInt32(Console.ReadLine()); int[] arr = { 1, 5, 7, -1, 5 }; for(int i = 0; i < arr.Length; i++) { for(int j = i+1; j < arr.Length; j++) { if(sum == arr[i] + arr[j]) { Console.WriteLine("Sum pairs:" + i + "," + j); } } } Console.ReadKey(); } } } Output Enter sum: 0 Sum pairs:0,3

Move all the negative elements to one side of the array in C#

Solution 1: / C# program to move all negative numbers to the // beginning and all positive numbers to the end with // constant extra space using System ; using System.Collections ; public class Gfg { public static void Main () { int [] arr = { - 1 , 2 , - 3 , 4 , 5 , 6 , - 7 , 8 , 9 }; Array . Sort ( arr ); foreach ( int e in arr ) Console . Write ( e + " " ); } } // This code is contributed by Saurabh Jaiswal Output -7 -3 -1 2 4 5 6 8 9   

Write a program to cyclically rotate an array by one in C#

// C# code for program to cyclically // rotate an array by one using System;   public class Test {      static int [] arr = new int [] { 1, 2, 3, 4, 5 };        // Method for rotation      static void rotate()      {          int last_el = arr[arr.Length - 1], i;            for (i = arr.Length - 1; i > 0; i--)              arr[i] = arr[i - 1];            arr[0] = last_el;      }        // Driver Code      public static void Main()      {          Console.WriteLine( "Given Array is" );          string Original_array = string .Join( " "...