Program number 1 : Remove Duplicates from an Array L
public static void Main(string[] args)
{
int[] withDuplicates= {1,2,3,4,5,1,2};
int[] uniqueNums = RemoveDuplicates(withDuplicates);
foreach(int num in uniqueNums){
Console.WriteLine(num);
}
}
static int[] RemoveDuplicates(int[] arr){
//Use HashSet to store unique element
HashSet<int> uniqueSet= new HashSet<int>();
foreach(int arrs in arr){
uniqueSet.Add(arrs);
}
//Convert the hasset back to array
int[] uniqueArray = new int[uniqueSet.Count];
uniqueSet.CopyTo(uniqueArray);
return uniqueArray;
}
Program number 2 : Print Sum of integer in an array
static int SumOfArray(int[] nums)
{
int sum = 0;
foreach(int num in nums)
{
sum+=num;
}
return sum;
}
Program to Print the sorted array :
static void PrintArray(int[] nums){
foreach(int num in nums){
Console.WriteLine(num);
}
Console.WriteLine();
}
static void BubbleSort(int[] numbers){
int n= numbers.Length;
for(int i=0; i< n-1; i++){
//Last i element are already Sorted
for(int j=0; j< n-1-i; j++){
//Swap is num greater than next numbers
if(numbers[j] > numbers[j+1]){
Swap(numbers,j,j+1);
}
}
}
}
static void Swap(int[] array, int index1, int index2)
{
int temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
Program to find position of array giving the targeted sum
static int[] FindTwoSum(int[] numbers, int targetValue)
{
for(int i=0; i<numbers.Length; i++){
for(int j=i+1; j<numbers.Length; j++){
if(numbers[i] + numbers[j] == targetValue)
{
return new int[] {i,j};
}
}
}
return null;
Program to Print Nth Highest Element in the Array :
static int NHighestValue(int[] array, int n)
{
Array.Sort(array);
Array.Reverse(array);
if(n>0 && n<array.Length)
{
return array[n-1];
}else
{
throw new ArgumentOutOfRangeException("n", "N must be within the range of the array
length.");
}
}
Program to Reverse the Array using loop :
static void ReverseArray(int[] array)
17 {
18 int left = 0;
19 int right = array.Length - 1;
20
21 while (left < right)
22 {
23 // Swap elements
24 int temp = array[left];
25 array[left] = array[right];
26 array[right] = temp;
27
28 // Move towards the middle
29 left++;
30 right--;
31 }
Program to reverse words in String :
static string ReverseWorld(string str)
{
//Split the string into words
string[] words = str.Split(' ');
//then reverse each word and store it in a array
for(int i=0; i < words.Length ; i++)
{
char[] charArray = words[i].ToCharArray();
Array.Reverse(charArray);
words[i] = new string(charArray);
}
//Join the reverse words back into single string
return string.Join(" ", words);
}
Program to reverse the string :
static string ReverseString(string str)
{
char[] charArray = str.ToCharArray();
string reversed = "";
for(int i=charArray.Length -1 ; i > 0; i--)
{
reversed+=charArray[i];
}
return reversed;
}
Program to print non repeating char from a string in C# :
static List<char> FindNonRepeatingCharacters(string str)
{
//Dictored to store non repeating char
Dictionary<char, int> charCount = new Dictionary<char,int>();
//To find the occurence of char
foreach(char c in str)
{
if(charCount.ContainsKey(c)){
charCount[c]++;
}else
{
charCount[c] =1;
}
}
List<char> nonRepeatingChars= new List<char>();
//find all non repeating nonRepeatingChars
foreach(char c in str){
if(charCount[c] == 1)
{
nonRepeatingChars.Add(c);
}
}
return nonRepeatingChars;
} }
Program to remove duplicates from string :
static string RemoveDuplicates(string str)
{
string result = string.Empty;
foreach(char c in str)
{
if(!result.Contains(c))
{
result += c;
}
}
return result;
}
Triangle Pattern :
public class Program
{
static void printTriangle(int n){
int space = n-1;
for(int i = 0; i < n; i++){
for(int j =0; j < space ; j++){
Console.Write(" ");
}
for(int j=0; j<= i; j++){
Console.Write("* ");
}
Console.WriteLine();
space--;
}