public void ClearShouldRemoveElement()
        {
            var hashtable = new HashSet<int>();

            hashtable.Add(10);

            Assert.IsTrue(hashtable.Find(10));

            hashtable.Clear();

            Assert.IsFalse(hashtable.Find(10));
        }
        public void HashSetShouldFindElementsCorrectly()
        {
            var set = new HashSet<int>();
            set.Add(0);
            set.Add(1);
            set.Add(2);
            set.Add(3);

            Assert.IsTrue(set.Find(0));
            Assert.IsTrue(set.Find(1));
            Assert.IsTrue(set.Find(2));
            Assert.IsTrue(set.Find(3));
            Assert.IsFalse(set.Find(4));
        }
        public void FindShouldReturnTrueForAddedElement()
        {
            var hashtable = new HashSet<string>();

            hashtable.Add("aaa");

            Assert.IsTrue(hashtable.Find("aaa"));
        }
        public void HashSetShouldRemoveElementCorrectly()
        {
            var set = new HashSet<int>();
            set.Add(0);
            set.Remove(0);

            Assert.IsFalse(set.Find(0));
            Assert.AreEqual(0, set.Count);
        }
Example #5
0
 static void Main(string[] args)
 {
     try
     {
         HashSet<int> set = new HashSet<int>();
         set.Add(13);
         set.Add(14);
         set.Add(15);
         set.Add(16);
         set.Add(17);
         Console.WriteLine(set.Find(14));
         Console.WriteLine("Count: {0}", set.Count);
         set.Remove(14);
         Console.WriteLine("Count: {0}", set.Count);
         Console.WriteLine(set.Find(14));
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
Example #6
0
        public static void Main()
        {
            try
            {
                HashSet<int> hashSet = new HashSet<int>();

                hashSet.Add(1);
                hashSet.Add(5);
                hashSet.Add(2);
                hashSet.Add(33);
                Console.WriteLine("Count is : {0}", hashSet.Count);

                hashSet.Remove(2);
                Console.WriteLine("Count is : {0}", hashSet.Count);

                Console.WriteLine(hashSet.Find(5));
                Console.WriteLine(hashSet.Contains(5));

                int[] numArray = new int[] { 5, 11, 33 };
                int[] array = numArray;
                hashSet.UnionWith(array);
                Console.WriteLine("Count is : {0}", hashSet.Count);

                int[] numArray1 = new int[] { 5, 11, 22 };
                int[] array2 = numArray1;
                hashSet.Intersect(array2);
                Console.WriteLine("Count is : {0}", hashSet.Count);

                hashSet.Clear();
                Console.WriteLine("Count is : {0}", hashSet.Count);
                Console.WriteLine(hashSet.Find(5));
            }
            catch (ArgumentException aex)
            {
                Console.WriteLine(aex.Message);
            }
        }
        static void Main(string[] args)
        {
            HashSet<string> hashSet = new HashSet<string>();

            hashSet.Add("ooo");
            hashSet.Add("qqq");
            hashSet.Add("ppp");
            hashSet.Add("iii");

            foreach (var item in hashSet.Items)
            {
                Console.WriteLine(item);
            }

            hashSet.Remove("iii");
            Console.WriteLine("\nCount after removal: {0}", hashSet.Count);


            Console.WriteLine();
            Console.WriteLine(hashSet.Find("ppp"));


            HashSet<string> secondHashSet = new HashSet<string>();
            secondHashSet.Add("www");
            secondHashSet.Add("qqq");
            secondHashSet.Add("yyy");
            secondHashSet.Add("ooo");

            Console.WriteLine("\nUnion: ");
            HashSet<string> union = hashSet.Union(secondHashSet);
            foreach (var item in union.Items)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("\nIntersection: ");
            HashSet<string> intersection = hashSet.Intersect(secondHashSet);
            foreach (var item in intersection.Items)
            {
                Console.WriteLine(item);
            }
        }
Example #8
0
        static void Main(string[] args)
        {
            IHashSet<int> sampleOne = new HashSet<int>();
           
            sampleOne.Add(1);
            sampleOne.Add(1);
            sampleOne.Add(2);
            sampleOne.Add(3);
            sampleOne.Add(4);
            sampleOne.Add(5);
            sampleOne.Add(6);
            sampleOne.Add(7);
            sampleOne.Add(8);
            sampleOne.Add(9);

             IHashSet<int> sampleTwo = new HashSet<int>();

            sampleTwo.Add(1);
            sampleTwo.Add(2);
            sampleTwo.Add(9);
            sampleTwo.Add(100);


            Console.WriteLine("sampleOne:");
            foreach (var item in sampleOne)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("sampleTwo:");
            foreach (var item in sampleTwo)
            {
                Console.WriteLine(item);
            }


            IHashSet<int> intersected = sampleOne.Intersect(sampleTwo);

            Console.WriteLine("Intesected of sampleOne and sampleTwo:");
            foreach (var item in intersected)
            {
                Console.WriteLine(item);
            }

            IHashSet<int> union = sampleOne.Union(sampleTwo);

            Console.WriteLine("Union of sampleOne and sampleTwo:");
            foreach (var item in union)
            {
                Console.WriteLine(item);
            }

            int founded = sampleOne.Find(1);
            Console.WriteLine("Look for '1':" + founded);

            Console.WriteLine("Count:" + sampleOne.Count);
            sampleOne.Remove(3);
            sampleOne.Clear();
            Console.WriteLine("Count after clear:" + sampleOne.Count);

        }
        public void FindShouldReturnFalseIfKeyIsNotAdded()
        {
            var hashtable = new HashSet<string>();

            Assert.IsFalse(hashtable.Find("aaa"));
        }
        public void RemoveShouldWorkProperly()
        {
            var hashtable = new HashSet<int>();

            hashtable.Add(10);

            Assert.IsTrue(hashtable.Find(10));

            hashtable.Remove(10);

            Assert.IsFalse(hashtable.Find(10));
        }