Advertisement
kirilstanoev

Untitled

Mar 9th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.02 KB | None | 0 0
  1. class Program
  2.     {
  3.         static void Main()
  4.         {
  5.             MySet<string> hashSet = new MySet<string>();
  6.             while (true)
  7.             {
  8.                 string input = Console.ReadLine();
  9.                 hashSet.Add(input);
  10.             }
  11.         }
  12.     }
  13.     class MySet<T>
  14.     {
  15.         private List<T>[] items = new List<T>[100];
  16.  
  17.         public void Add(T item)
  18.         {
  19.             int index = this.GetIndex(item.GetHashCode());
  20.  
  21.             if (this.Contains(item, index))
  22.             {
  23.                 return;
  24.             }
  25.  
  26.             if (items[index] == null)
  27.             {
  28.                 items[index] = new List<T>();
  29.             }
  30.  
  31.             items[index].Add(item);
  32.         }
  33.  
  34.         public bool Contains(T item)
  35.         {
  36.             int hashCode = item.GetHashCode();
  37.             int index = this.GetIndex(hashCode);
  38.             return this.Contains(item, index);
  39.         }
  40.  
  41.         private int GetIndex(int hashcode)
  42.         {
  43.             return Math.Abs(hashcode % items.Length);
  44.         }
  45.  
  46.         private bool Contains(T value, int index)
  47.         {
  48.             if (this.items[index] != null)
  49.             {
  50.                 foreach (T item in this.items[index])
  51.                 {
  52.                     if (item.Equals(value))
  53.                     {
  54.                         return true;
  55.                     }
  56.                 }
  57.             }
  58.                
  59.             return false;
  60.         }
  61.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement