Hash Functions and Hash Tables

Alex Onsman
Updated on 22-Jun-2020 10:03:08

13K+ Views

Hashing is the process of generating a value from a text or a list of numbers using a mathematical function known as a hash function.There are many hash functions that use numeric numeric or alphanumeric keys. Different hash functions are given below:Hash FunctionsThe following are some of the Hash Functions −Division MethodThis is the easiest method to create a hash function. The hash function can be described as −h(k) = k mod nHere, h(k) is the hash value obtained by dividing the key value k by size of hash table n using the remainder. It is best that n is ... Read More

Find Volume and Surface Area of a Sphere using C#

Samual Sam
Updated on 22-Jun-2020 10:00:28

394 Views

For volume and surface area of a sphere, firstly declare a variable with the value of radius.int r = 15;Get the volume f the sphere.// calculating volume of sphere cal_volume = (4.0 / 3) * (22 / 7) * r * r * r;Now the surface area of the sphere is calculated −cal_area = 4 * (22 / 7) * r * r;Let us see the complete code −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main(string[] args) {       double cal_area, cal_volume, r;       // radius       ... Read More

Find the Size of a List in C#

Chandu yadav
Updated on 22-Jun-2020 09:59:59

3K+ Views

Declare and initialize a list.var products = new List (); products.Add("Accessories"); products.Add("Clothing"); products.Add("Footwear");To get the size, use the Capacity property.products.CapacityNow let us see the complete code to find the size of a list.Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main(string[] args) {       var products = new List ();       products.Add("Accessories");       products.Add("Clothing");       products.Add("Footwear");       Console.WriteLine("Our list....");       foreach(var p in products) {          Console.WriteLine(p);       }       Console.WriteLine("Size of list = " + products.Capacity);    } }OutputOur list.... Accessories Clothing Footwear Size of list = 4

Check If a Number is Divisible by 2 Using C#

George John
Updated on 22-Jun-2020 09:59:28

273 Views

To find whether a number is divisibe by 2 is not, check what happens when the number is divisible by 2.If the remainder is 0, then the number is divisible by 2, else it is false −if (num % 2 == 0) {    Console.WriteLine("Divisible by 2 "); } else {    Console.WriteLine("Not divisible by 2"); }The following is the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo {    class Test {       static void Main(string[] args) {          int num;          num = 18; ... Read More

Find Most Frequent Element in C#

karthikeya Boyini
Updated on 22-Jun-2020 09:58:31

1K+ Views

Let’s say our string is −String s = "HeathLedger!";Now create a new array.int []cal = new int[maxCHARS];Create a new method and pass both the string and the new array in it. Find the maximum occurrence of a character.static void calculate(String s, int[] cal) {    for (int i = 0; i < s.Length; i++)    cal[s[i]]++; }Let us see the complete code −Example Live Demousing System; class Demo {    static int maxCHARS = 256;    static void calculate(String s, int[] cal) {       for (int i = 0; i < s.Length; i++)       cal[s[i]]++;    } ... Read More

Declare a Const Array in C#

Ankith Reddy
Updated on 22-Jun-2020 09:57:59

12K+ Views

In C#, use readonly to declare a const array.public static readonly string[] a = { "Car", "Motorbike", "Cab" };In readonly, you can set the value at runtime as well unlike const.Another alternative of achieving what we saw above −public ReadOnlyCollection a { get { return new List { "Car", "Motorbike", "Cab" }.AsReadOnly();}} .NET framework 4.5 brings an improvement to what we saw −public ReadOnlyCollection a { get; } = new ReadOnlyCollection( new string[] { "Car", "Motorbike", "Cab" } );

Replace Special Character from a String in C#

karthikeya Boyini
Updated on 22-Jun-2020 09:57:38

6K+ Views

Let’s say our string is −string str = "abcd$ef$gh";To replace the special character, use the Replace() method.string res = str.Replace('$', 'k');The following is the complete code to replace character from a string −Example Live Demousing System; public class Program {    public static void Main() {       string str = "abcd$ef$gh";       Console.WriteLine("Initial string = " + str);       string res = str.Replace('$', 'k');       // after replacing       Console.WriteLine("Replaced string = " + res.ToString());    } }OutputInitial string = abcd$ef$gh Replaced string = abcdkefkgh

Replace Character with Asterisks in a Sentence using C#

Ankith Reddy
Updated on 22-Jun-2020 09:57:18

1K+ Views

Use the Replace() method to replace a character with asterisks.Let’s say our string is −string str = "dem* text";To replace it, use the Replace() method −str.Replace('*', 'o');Here is the complete code −Example Live Demousing System; public class Program {    public static void Main() {       string str = "dem* text";       Console.WriteLine("Initial string = " + str);       string res = str.Replace('*', 'o');       // after replacing       Console.WriteLine("After replacing asterisk = " + res.ToString());    } }OutputInitial string = dem* text After replacing asterisk = demo text

Find Average of Elements in an Integer Array in C#

Samual Sam
Updated on 22-Jun-2020 09:56:58

266 Views

The following is our integer array −int[] myArr = new int[6] {    8,    4,    2,    5,    9,    14 };Firstly, get the length of the array, and loop through the array to find the sum of the elements. After that, divide it with the length.int len = myArr.Length; int sum = 0; int average = 0; for (int i = 0; i < len; i++) {    sum += myArr[i]; } average = sum / len;Here is the complete codeExample Live Demousing System; public class Program {    public static void Main() {       ... Read More

Find First 10 Characters of a String in C#

karthikeya Boyini
Updated on 22-Jun-2020 09:56:07

15K+ Views

To get the first 10 characters, use the substring() method.Let’s say the following is our string −string str = "Cricket is a religion in India!";Now to get the first 10 characters, set the value 10 in the substring() method as shown below −string res = str.Substring(0, 10);Let us see the complete code.Example Live Demousing System; public class Demo {    public static void Main() {       string str = "Cricket is a religion in India!";       string res = str.Substring(0, 10);       Console.WriteLine(res);    } }OutputCricket is

Advertisements