Found 34488 Articles for Programming

C# program to find common values from two or more Lists

George John
Updated on 22-Jun-2020 13:00:17

250 Views

Create more than one list −// two lists var list1 = new List{3, 4}; var list2 = new List{1, 2, 3};Now, use the Intersect() method to get the common values −var res = list1.Intersect(list2);The following is the complete code −Example Live Demousing System.Collections.Generic; using System.Linq; using System; public class Demo {    public static void Main() {       // two lists       var list1 = new List{3, 4};       var list2 = new List{1, 2, 3};       // common values       var res = list1.Intersect(list2);       foreach(int i in res) {          Console.WriteLine(i);       }    } }Output3

C# program to concat two or more Lists

Chandu yadav
Updated on 22-Jun-2020 13:00:54

165 Views

Set three lists −// three lists var list1 = new List{3, 4}; var list2 = new List{1, 2, 3}; var list3 = new List{2, 5, 6};Now, use the Concat mthod to concat the above lists −var res1 = list1.Concat(list2); var res2 = res1.Concat(list3);Here is the complete code −Example Live Demousing System.Collections.Generic; using System.Linq; using System; public class Demo {    public static void Main() {       // three lists       var list1 = new List{3, 4};       var list2 = new List{1, 2, 3};       var list3 = new List{2, 5, 6};       // concat       var res1 = list1.Concat(list2);       var res2 = res1.Concat(list3);       foreach(int i in res2) {          Console.WriteLine(i);       }    } }Output3 4 1 2 3 2 5 6

C# program to find Union of two or more Lists

Arjun Thakur
Updated on 22-Jun-2020 13:01:22

904 Views

Firstly, create lists −//three lists var list1 = new List{3, 4, 5}; var list2 = new List{1, 2, 3, 4, 5}; var list3 = new List{5, 6, 7, 8};Use the union method to get the union of list1 and list2 −var res1 = list1.Union(list2); var res2 = res1.Union(list3);The following is the complete code −Example Live Demousing System.Collections.Generic; using System.Linq; using System; public class Demo {    public static void Main() {       //three lists       var list1 = new List{3, 4, 5};       var list2 = new List{1, 2, 3, 4, 5};       var list3 = new List{5, 6, 7, 8};       // finding union       var res1 = list1.Union(list2);       var res2 = res1.Union(list3);       foreach(int i in res2) {          Console.WriteLine(i);       }    } }Output3 4 5 1 2 6 7 8

C# program to find Union of two or more Dictionaries

Chandu yadav
Updated on 22-Jun-2020 13:01:54

507 Views

Firstly, set both the dictionaries −Dictionary < string, int > dict1 = new Dictionary < string, int > (); dict1.Add("water", 1); dict1.Add("food", 2); Dictionary < string, int > dict2 = new Dictionary < string, int > (); dict2.Add("clothing", 3); dict2.Add("shelter", 4);Now, create HashSet and use UnionsWith() method to find the union between the above two Dictionaries −HashSet < string > hSet = new HashSet < string > (dict1.Keys); hSet.UnionWith(dict2.Keys);The following is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Program {    public static void Main() {       Dictionary < string, int > dict1 = ... Read More

How to check if two Strings are anagrams of each other using C#?

Samual Sam
Updated on 22-Jun-2020 13:02:55

1K+ Views

Under anagram, another string would have the same characters present in the first string, but the order of characters can be different.Here, we are checking the following two strings −string str1 = "silent"; string str2 = "listen";Convert both the strings into character array −char[] ch1 = str1.ToLower().ToCharArray(); char[] ch2 = str2.ToLower().ToCharArray();Now, sort them −Array.Sort(ch1); Array.Sort(ch2);After sorting, convert them to strings −string val1 = new string(ch1); string val2 = new string(ch2);Compare both the strings for equality. If both are equal, that would mean they are anagrams.The following is the code −Example Live Demousing System; public class Demo {    public static ... Read More

C# program to check whether a given string is Heterogram or not

karthikeya Boyini
Updated on 22-Jun-2020 13:03:33

298 Views

Heterogram for a string means the string isn’t having duplicate letters. For example −Mobile Cry LaptopLoop through each word of the string until the length of the string −for (int i = 0; i < len; i++) {    if (val[str[i] - 'a'] == 0)    val[str[i] - 'a'] = 1;    else    return false; }Above, len is the length of the string.Let us see the complete code −Example Live Demousing System; public class GFG {    static bool checkHeterogram(string str, int len) {       int []val = new int[26];       for (int i ... Read More

How to Convert Hex String to Hex Number in C#?

karthikeya Boyini
Updated on 22-Jun-2020 13:04:03

1K+ Views

Firstly, set the Hex String −string str = "7D";Now, use the Convert.ToSByte() method to convert the Hex string to Hex number −Console.WriteLine(Convert.ToSByte(str, 16));Let us see the complete code −Example Live Demousing System; namespace Demo {    public class Program {       public static void Main(string[] args) {          string str = "7D";          Console.WriteLine(Convert.ToSByte(str, 16));       }    } }Output125Another way of converting Hex String to Hex Number −Example Live Demousing System; namespace Demo {    public class Program {       public static void Main(string[] args) {          string str = "7D";          Console.WriteLine(Convert.ToInt32(str, 16));       }    } }Output125

How to convert a tuple into an array in C#?

Samual Sam
Updated on 22-Jun-2020 12:53:45

739 Views

Firstly, set a tuple −Tuple t = Tuple.Create(99,53);Now, convert the tuple to an array −int[] arr = new int[]{t.Item1, t.Item2};The following is the code to convert a tuple into an array −Example Live Demousing System; using System.Linq; using System.Collections.Generic; namespace Demo {    public class Program {       public static void Main(string[] args) {          Tuple t = Tuple.Create(99,53);          int[] arr = new int[]{t.Item1, t.Item2};          foreach (int val in arr) {             Console.WriteLine(val);          }       }    }   }Output99 53

How to convert a number from Decimal to Binary using recursion in C#?

karthikeya Boyini
Updated on 22-Jun-2020 12:54:49

365 Views

To get the binary of Decimal, using recursion, firstly set the decimal number −int dec = 30;Now pass the value to a function −public int displayBinary(int dec) { }Now, check the condition until the decimal value is 0 and using recursion get the mod 2 of the decimal num as shown below. The recursive call will call the function again with the dec/2 value −public int displayBinary(int dec) {    int res;    if (dec != 0) {       res = (dec % 2) + 10 * displayBinary(dec / 2);       Console.Write(res);       return ... Read More

Merge two sorted arrays into a list using C#

Samual Sam
Updated on 22-Jun-2020 12:55:32

192 Views

To merge two sorted arrays into a list, firstly set two sorted arrays −int[] array1 = { 1, 2 }; int[] array2 = { 3, 4 };Add it to a list and merge −var list = new List(); for (int i = 0; i < array1.Length; i++) {    list.Add(array1[i]);       list.Add(array2[i]); }Now, use the ToArray() method to convert back into an array as shown below −Example Live Demousing System; using System.Collections.Generic; public class Program {    public static void Main() {       int[] array1 = { 56, 70, 77};       int[] array2 = ... Read More

Advertisements