Notes
![]() ![]() Notes - notes.io |
using System.Collections.Immutable;
using System.Diagnostics.Contracts;
using System.Security.Cryptography.X509Certificates;
namespace AlgoPractice;
class Program
{
public static void Main(string[] args)
{
MergeArray();
//DescOrder();
//string rev = "";
//for(int i = str.Length-1;i>=0; i--)
//{
// rev += str[i] ;
//}
//Console.WriteLine(rev);
}
//Merge two sorted array
public static void MergeArray()
{
int[] arr1 = new int[10];
int[] arr2 = new int[10];
Console.WriteLine("First array");
for (int i = 0; i < 10; i++)
{
arr1[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Second array");
for (int i = 0; i < 10; i++)
{
arr2[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Merge array");
int[] arr3 = new int[20];
Array.Sort(arr1);
Array.Sort(arr2);
int n = 10;
int m = 10;
while (n > 0)
{
arr3[n-1] = arr1[n-1];
n--;
}
while (m > 0)
{
arr3[m+n-1] = arr2[m+n-1];
m--;
}
for(int i=0;i<arr3.Length;i++)
{
Console.WriteLine(arr3[i]);
}
}
//Majority element in an array
public static void MajorityElement()
{
int[] arr = { 1, 2, 3, 3, 4, 6, 7, 7, 7,7,8, 8 };
int[] maj = new int[arr.Length];
for(int i=0;i<arr.Length;i++)
{
maj[arr[i]]++;
}
int max = 0;
for(int i = 0; i < arr.Length; i++)
{
if (max < maj[arr[i]])
{
max = arr[i];
}
}
Console.WriteLine(max);
}
//sort an array in descending order
public static void DescOrder()
{
int[] arr = { 1, 25, 3, 4, 58, 63, 76, };
for(int i=0;i<arr.Length;i++)
{
for (int j = i; j < arr.Length; j++)
{
if (arr[i] < arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
Console.WriteLine("Sorted array is:");
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
Console.WriteLine("Reversed array is:");
Array.Reverse(arr);
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
}
//two array sum results into target
public static bool TwoSum()
{
int[] arr = { 1, 2, 3, 4, 5, 6 };
int target = 7;
for(int i=0;i<arr.Length;i++)
{
for(int j = i+1; j < arr.Length; j++)
{
if (arr[i] + arr[j] == target)
{
return true;
}
}
}
return false;
}
//all substrings in a string
public static string Sub(string s)
{
string subs = "";
for(int i=0;i<s.Length;i++)
{
for(int j = 0; j < s.Length - i; j++)
{
subs = subs + " " + s.Substring(i,j);
}
}
return subs;
}
//replace all spaces with %20
public static string ReplaceSpace(string s)
{
string res = s.Replace(" ", "%20");
return res;
}
//all unique characters
public static bool uniqueChar(string s)
{
int[] arr= new int[10000];
for(int i=0;i<s.Length;i++)
{
arr[s[i]]++;
}
for(int i=0;i< s.Length;i++)
{
if (arr[s[i]] != 1)
{
return false;
}
}
return true;
}
//Highest occurring character
public static char HighOccurence(string str)
{
int[] maxCount = new int[2000];
int count = 0;
char rev = ' ';
for (int i = 0; i < str.Length; i++)
{
maxCount[str[i]]++;
}
for (int i = 0; i < str.Length; i++)
{
if (count < maxCount[str[i]])
{
count = maxCount[str[i]];
rev = str[i];
}
}
return rev;
}
//Duplicate characters
public static string RemoveDuplicate(string s)
{
if (s == null)
{
Console.WriteLine("string is empty");
}
string res = new string(s.Distinct().ToArray());
return res;
}
//string Palindrome
public static bool Palindrome(string str)
{
bool result = false;
for(int i=0;i<str.Length-1;i++)
{
if (str[i] == str[(str.Length-1) - i])
{
result = true;
}
}
return result;
}
//count words in a string
public static int CountString(string str)
{
int count = 0;
str = str.Trim();
if(str == "")
{
return 0;
}
while(str.Contains(" "))
{
str.Replace(" ", " ");
}
foreach(string s in str.Split(' '))
{
count++;
}
return count;
}
//reverse recursion
public static string Reverse(string str)
{
if (str.Length == 1)
{
return str;
}
return str.Last()+Reverse(str.Substring(0,str.Length-1));
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team