static void Main(string[] args) { Console.WriteLine("Welcome to HashTable Program"); MyMapNode <int, string> hash = new MyMapNode <int, string>(15);//creating object and Key and value datatype is string and size is 5 //creating a string of sentence string phrase = "Paranoids are not paranoid because they are paranoid but because they keep putting themselves deliberately into paranoid avoidable situations"; //adding values into array string[] words = phrase.Split(' '); //split method is used //split string into substrings int a = 1; foreach (var element in words) { hash.Add(a, element); a++; } int index = 18; string choice = hash.Get(index); // getting the specific value from hashtable. Console.WriteLine("{0}th index values : is {1}", index, choice); int index2 = 15; string choice2 = hash.Get(index2); Console.WriteLine("{0}th index values : is {1}", index2, choice2); hash.Remove(index);//remove 18 index hash.Display(); Console.Read(); }
/// <summary> ///Display the count the of words provided. /// </summary> /// <param name="hash">The hash.</param> public void FrequencyOfWord(MyMapNode <int, string> hash) { for (int key = 0; key < hash.size; key++) { frequency.TryAdd(hash.Get(key).ToLower(), 0); frequency[hash.Get(key).ToLower()]++; } foreach (KeyValuePair <string, int> item in frequency) { Console.WriteLine($"frequency of word '{item.Key}' is {item.Value}"); } }
public static void Main(string[] args) { Console.WriteLine("Welcome to Hash Table implementation"); MyMapNode <string, string> hash = new MyMapNode <string, string>(5); hash.Add("0", "To"); hash.Add("1", "be"); hash.Add("2", "or"); hash.Add("3", "not"); hash.Add("4", "To"); hash.Add("5", "be"); string hash5 = hash.Get("4"); Console.WriteLine("5th index value:" + hash5); string paragraph = "Paranoids are not paranoid because they are paranoid but because they keep putting themselves deliberately into paranoid avoidable situations"; ///Split converts paragraph into array of sub strings. string[] para = paragraph.Split(" "); ///Creating reference of MyMapNode. MyMapNode <int, string> hash1 = new MyMapNode <int, string>(para.Length); int key = 0; ///foreach iterates on paragraph and adds key and value to hash. foreach (string word in para) { hash1.Add(key, word); key++; } Console.WriteLine("\nFrequency :" + hash1.GetFrequency("paranoid")); hash1.RemoveValue("avoidable"); Console.WriteLine("Frequency :" + hash.GetFrequency("avoidable")); }
static void Main(string[] args) { Console.WriteLine("-----Welcome To HashTable Program-----"); MyMapNode <string, int> hash = new MyMapNode <string, int>(5); //creating a string of long sentence string words = "Paranoids are not paranoid because they are paranoid but because they keep putting themselves deliberately into paranoid avoidable situations"; //adding values into array string[] arr = words.Split(' '); //creating a linkedlist to check for duplicates value LinkedList <string> checkForDuplication = new LinkedList <string>(); //iterating loop over the array of sentence words foreach (string element in arr) { int count = 0; //another foreach loop to check if other same element is present //if present counter is incremented foreach (string match in arr) { if (element == match) { count++; //if same element is coming in 2nd time, it will be presnent in linkedlist, so loopwill break and no further counting will be done if (checkForDuplication.Contains(element)) { break; } } } //if element is already there in list, outer loop will be continued and loop will shift to next value //basically only values appearing for 1st time, will be added in linkedlist and added in hash table, so that frequency can be displayed. if (checkForDuplication.Contains(element)) { continue; } //added element in linkedlist checkForDuplication.AddLast(element); //added element and it's frequency in hashtable. hash.Add(element, count); } //getting the specific value from hashtable. int frequency = hash.Get("they"); Console.WriteLine("frequency for they:\t" + frequency); //Displaying all the elements from the linkedlist Console.WriteLine(); Console.WriteLine("Displaying all the key value pairs in hash table"); hash.Display(); Console.Read(); }
/// <summary> /// Remove word from paragraph. /// </summary> /// <param name="hash"></param> /// <param name="word"></param> public void Remove(MyMapNode <int, string> hash, string word) { for (int key = 0; key < hash.size; key++) { if (hash.Get(key).Equals(word)) { hash.Remove(key); Console.WriteLine("You have removed word -----> " + word + " : from paragraph."); } } }
public void Remove(MyMapNode <int, string> hash, string word) { for (int key = 0; key < hash.size; key++) { if (hash.Get(key).Equals(word)) { hash.Remove(key); Console.WriteLine($"Removed {word} from paragraph"); } } }
/// <summary> /// Removes the specified hash. /// </summary> /// <param name="hash">The hash.</param> /// <param name="word">The word.</param> public void Remove(MyMapNode <int, string> hash, string word) { //Loop iterates across the hash to compare key with the word //if passed word matches with the key then hash.remove removes that value for (int key = 0; key < hash.size; key++) { if (hash.Get(key).Equals(word)) { hash.Remove(key); Console.WriteLine($"Removed {word} from paragraph"); } } }
static void Main(string[] args) { Console.WriteLine("Welcome to Hash Table Demo.."); MyMapNode <string, string> hash = new MyMapNode <string, string>(18); hash.Add("0", "Paranoids"); hash.Add("1", "are"); hash.Add("2", "not"); hash.Add("3", "paranoid"); hash.Add("4", "because"); hash.Add("5", "they"); hash.Add("6", "are"); hash.Add("7", "paranoid"); hash.Add("8", "but"); hash.Add("9", "because"); hash.Add("10", "they"); hash.Add("11", "keep"); hash.Add("12", "putting"); hash.Add("13", "themselves"); hash.Add("14", "deliberately"); hash.Add("15", "into"); hash.Add("16", "paranoid"); hash.Add("17", "avoidable"); hash.Add("18", "situations"); string hash3 = hash.Get("3"); Console.WriteLine("3rd index value: " + hash3); string hash2 = hash.Get("2"); Console.WriteLine("2nd index value: " + hash2); string hash17 = hash.Get("17"); Console.WriteLine("17th index value: " + hash17); hash.Remove("17"); Console.WriteLine("17th index value removed.. "); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); MyMapNode <string, string> hash = new MyMapNode <string, string>(5); hash.Add("0", "To"); hash.Add("1", "Be"); hash.Add("2", "Or"); hash.Add("3", "Not"); hash.Add("4", "To"); hash.Add("5", "Be"); string hash5 = hash.Get("5"); Console.WriteLine("Element present at 5th index:{0}", hash5); }
static void Main(string[] args) { Console.WriteLine("Welcome to Hash Table"); //Declaring MyMapNode object with data type and size of array in parametrized constructor MyMapNode <string, int> hash = new MyMapNode <string, int>(5); //Adding values in hashtable. //hash.Add("0", "To"); //hash.Add("1", "be"); //hash.Add("2", "or"); //hash.Add("3", "not"); //hash.Add("4", "To"); //hash.Add("5", "be"); //getting the specific value from hashtable. //string hash5 = hash.Get("5"); //Console.WriteLine("5th index value:" + hash5); //creating a string of long sentence string words = "Paranoids are not paranoid because they are paranoid but because they keep putting themselves deliberately into paranoid avoidable situations"; //adding values into array string[] arr = words.Split(" "); //creating a linkedlist to check for duplicates value //if duplicate value is coming inside a array, as previous element(when it was not in list) would already contain total count of same elements //it will be skipped LinkedList <string> checkForDuplication = new LinkedList <string>(); //iterating loop over the array of sentence words foreach (string element in arr) { //count iterator for counting no of times word is coming int count = 0; //another foreach loop to check if other same element is present //if present counter is incremented foreach (string match in arr) { if (element == match) { count++; //if same element is coming in 2nd time, it will be presnent in linkedlist, so loopwill break and no further counting will be done if (checkForDuplication.Contains(element)) { break; } } } //if element is already there in list, outer loop will be continued and loop will shift to next value //basically only values appearing for 1st time, will be added in linkedlist and added in hash table, so that frequency can be displayed. if (checkForDuplication.Contains(element)) { continue; } //added element in linkedlist checkForDuplication.AddLast(element); //added element and it's frequency in hashtable. hash.Add(element, count); } //getting the specific value from hashtable. int frequency = hash.Get("Paranoids"); Console.WriteLine("frequency for Paranoids:\t" + frequency); //Displaying all the elements from the linkedlist Console.WriteLine("****************"); Console.WriteLine("Displaying all the key value pairs in hash table"); hash.Display(); Console.WriteLine("**********************************************"); //removing avoidable word from the hashtable hash.Remove("avoidable"); Console.WriteLine("Word removed from hashtable"); //getting the specific value from hashtable. int removedWordFrequency = hash.Get("avoidable"); Console.WriteLine("frequency for avoidable:\t" + removedWordFrequency); //Displaying all the elements from the linkedlist Console.WriteLine("****************"); Console.WriteLine("Displaying all the key value pairs in hash table"); hash.Display(); }
static void Main(string[] args) //main method { Console.WriteLine("Hash Table"); MyMapNode <string, int> hashtable = new MyMapNode <string, int>(5); hashtable.Display(); ///*UC1*/ //MyMapNode<string, string> hashtable = new MyMapNode<string, string>(5); //create MyMapNode class object adn passing key and value and size //hashtable.Add("0", "To"); //add key and value //hashtable.Add("1", "be"); //hashtable.Add("2", "or"); //hashtable.Add("3", "not"); //hashtable.Add("4", "to"); //hashtable.Add("5", "be"); //Console.WriteLine("\nShowing Value using Key using Get Method"); //Console.WriteLine($"{hashtable.Get("3")}"); //show the specific value from hashtable. //hashtable.Remove("5"); //Call Remove method //UC3 /* UC2:- Ability to find frequency of words in a large paragraph phrase “Paranoids are not * paranoid because they are paranoid but because they keep putting themselves * deliberately into paranoid avoidable situations” * - Use hashcode to find index of the words in the para. * - Create LinkedList for each index and store the words and its frequency. * - Use LinkedList to do the Hash Table Operation. * - To do this create MyMapNode with Key Value Pair and create LinkedList of MyMapNode. */ //MyMapNode<string, int> hashtable = new MyMapNode<string, int>(5); string input = "Paranoids are are not paranoid because they are paranoid but because they \n keep putting themselves deliberately into paranoid avoidable situations\n"; Console.WriteLine($"Statement is:- {input}"); hashtable.Display(); try { string[] inputArray = input.Split(); // split input statement using Split method foreach (string word in inputArray) //itterat word in inputArray { if (hashtable.Get(word) == 0) { hashtable.Add(word, 1); //Add ing key and value } else { int value = hashtable.Get(word) + 1; //add 1 hashtable.Set(word, value); //set word and value } } string[] newInputArray = inputArray.Distinct().ToArray(); //Unique value store not reapeat value foreach (var word in newInputArray) { Console.WriteLine("Frequency of Word ccurrence :- \"" + word + "\" is :- " + hashtable.Get(word)); //print word and count } } catch (Exception ex) { Console.WriteLine(ex.Message); } /* UC3:- Remove avoidable word from the phrase “Paranoids are not paranoid because they are paranoid but * because they keep putting themselves deliberately into paranoid avoidable situations” * - Use LinkedList to do the Hash Table Operation like here the removal of word avoidable. * - To do this create MyMapNode with Key Value Pair and create LinkedList of MyMapNode. */ hashtable.Remove("avoidable"); Console.ReadLine(); }