Example #1
0
        public void Intersection()
        {
            int[]           oddNumbers = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25 };
            int[]           digits     = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            HashedSet <int> setOdds    = new HashedSet <int>(oddNumbers);
            HashedSet <int> setDigits  = new HashedSet <int>(digits);

            setOdds.IntersectWith(setDigits);

            int[] expectedArray = new int[] { 1, 3, 5, 7, 9 };

            int[] actualArray = setOdds.ToArray();
            Array.Sort(actualArray);
            CollectionAssert.AreEqual(expectedArray, actualArray);

            setOdds = new HashedSet <int>(oddNumbers);

            setDigits.IntersectWith(setOdds);

            actualArray = setDigits.ToArray();
            Array.Sort(actualArray);
            CollectionAssert.AreEqual(expectedArray, actualArray);

            setOdds.IntersectWith(setOdds);
            Assert.AreEqual(oddNumbers.Length, setOdds.Count);
        }
Example #2
0
        public void IntersectShouldReturnSetWithElementsDoubledElelemt()
        {
            var firstSet  = new HashedSet <int>();
            var secondSet = new HashedSet <int>();

            for (int i = 0; i < 10; i++)
            {
                if (i < 7)
                {
                    firstSet.Add(i);
                }

                if (i > 3)
                {
                    secondSet.Add(i);
                }
            }

            var result = firstSet.IntersectWith(secondSet);

            Assert.AreEqual(3, result.Count);

            foreach (var item in result)
            {
                Assert.IsTrue(item >= 3 && item < 7);
            }
        }
Example #3
0
    static void Main()
    {
        Console.WriteLine("Adding 10 (numbers from 0 to 9) elements to the custom HashSet");
        HashedSet <int> testSet = new HashedSet <int>();

        for (int i = 0; i < 10; i++)
        {
            testSet.Add(i);
        }

        Console.Write("Printing all elements using foreach. Press any key: ");
        Console.ReadKey();
        PrintHashSet(testSet);

        Console.WriteLine("Adding 10 (numbers from 5 to 14 ) elements to another custom custom HashSet");
        HashedSet <int> testSet2 = new HashedSet <int>();

        for (int i = 5; i < 15; i++)
        {
            testSet2.Add(i);
        }

        Console.Write("Printing all elements using foreach. Press any key: ");
        Console.ReadKey();
        PrintHashSet(testSet2);

        Console.WriteLine("Printing intersection between the two sets (the first set is modified):");
        testSet.IntersectWith(testSet2);
        PrintHashSet(testSet);

        Console.WriteLine("Printing union between the two sets (the modified first set and the second set):");
        testSet.UnionWith(testSet2);
        PrintHashSet(testSet);
    }
Example #4
0
    static void Main()
    {
        HashedSet <int> set = new HashedSet <int>();

        set.Add(5);
        set.Add(5);
        set.Add(6);
        set.Add(1);
        set.Add(3);
        set.Add(3);
        set.Add(8);
        set.Remove(6);

        Console.WriteLine("First set contains 6 - {0}", set.Contains(6));
        Console.WriteLine("First set contains 8 - {0}", set.Contains(8));

        Console.WriteLine("First set");
        foreach (var item in set)
        {
            Console.Write("{0} ", item);
        }
        Console.WriteLine();

        Console.WriteLine("Second set");
        HashedSet <int> set2 = new HashedSet <int>();

        set2.Add(13);
        set2.Add(3);
        set2.Add(1);
        foreach (var item in set2)
        {
            Console.Write("{0} ", item);
        }
        Console.WriteLine();

        HashedSet <int> union = set.UnionWith(set2);

        Console.WriteLine("Union: ");
        foreach (var item in union)
        {
            Console.Write("{0} ", item);
        }
        Console.WriteLine();

        HashedSet <int> intersection = set.IntersectWith(set2);

        Console.WriteLine("Intersection: ");
        foreach (var item in intersection)
        {
            Console.Write("{0} ", item);
        }
        Console.WriteLine();
    }
Example #5
0
    static void Main()
    {
        HashedSet<int> set = new HashedSet<int>();
        set.Add(5);
        set.Add(5);
        set.Add(6);
        set.Add(1);
        set.Add(3);
        set.Add(3);
        set.Add(8);
        set.Remove(6);

        Console.WriteLine("First set contains 6 - {0}", set.Contains(6));
        Console.WriteLine("First set contains 8 - {0}", set.Contains(8));

        Console.WriteLine("First set");
        foreach (var item in set)
        {
            Console.Write("{0} ", item);
        }
        Console.WriteLine();

        Console.WriteLine("Second set");
        HashedSet<int> set2 = new HashedSet<int>();
        set2.Add(13);
        set2.Add(3);
        set2.Add(1);
        foreach (var item in set2)
        {
            Console.Write("{0} ", item);
        }
        Console.WriteLine();

        HashedSet<int> union = set.UnionWith(set2);
        Console.WriteLine("Union: ");
        foreach (var item in union)
        {
            Console.Write("{0} ", item);
        }
        Console.WriteLine();

        HashedSet<int> intersection = set.IntersectWith(set2);
        Console.WriteLine("Intersection: ");
        foreach (var item in intersection)
        {
            Console.Write("{0} ", item);
        }
        Console.WriteLine();
    }
        public void IntersectWithCollectionsWithNoCommonElementsShouldReturnNull()
        {
            testSet = new HashedSet <string>();
            testSet.Add("Pesho");
            testSet.Add("Gosho");

            var anotherTestSet = new HashedSet <string>();

            anotherTestSet.Add("Kiro");
            anotherTestSet.Add("Joro");

            var intersection = testSet.IntersectWith(anotherTestSet);

            Assert.IsNull(intersection);
        }
        public void TestIntersectMethod()
        {
            var hashedSet = new HashedSet<int>();

            Assert.IsTrue(hashedSet.Add(1));
            Assert.IsTrue(hashedSet.Add(2));
            Assert.IsTrue(hashedSet.Add(3));
            Assert.IsTrue(hashedSet.Add(4));
            Assert.AreEqual(4, hashedSet.Count);

            int[] intersectArray = { 2, 3 };
            hashedSet.IntersectWith(intersectArray);

            Assert.AreEqual(2, hashedSet.Count);
            CollectionAssert.AreEqual(hashedSet.Keys.ToList(), new List<int>() { 2, 3 });
        }
Example #8
0
        public void IntersectShouldProducesSetContainingCommonUniqueItems()
        {
            ICollection <int> numbers = new List <int>()
            {
                1, 2, 3, 4, 5, 6, 7, 8
            };

            var hashset = new HashedSet <int>();

            new int[] { 6, 7, 6, 6, 8, 9, 10, 11, 12 }.ForEach(x => hashset.Add(x));

            var result = hashset.IntersectWith(numbers);

            new int[] { 6, 7, 8 }.ForEach(x => Assert.IsTrue(result.Contains(x)));

            Assert.AreEqual(3, result.Count);
        }
    static void Main()
    {
        HashedSet<int> hashSet1 = new HashedSet<int>();

        for (int i = 0; i < 10; i++)
        {
            hashSet1.Add(i);
        }

        Console.WriteLine("Find element with key = 4");
        Console.WriteLine(hashSet1.Find(4));

        Console.WriteLine("Find element with key = 2 and write the count of elements");
        hashSet1.Remove(2);
        Console.WriteLine(hashSet1.Count);

        HashedSet<int> hashSet2 = new HashedSet<int>();
        hashSet2.Add(5);
        hashSet2.Add(9);
        hashSet2.Add(33);

        Console.WriteLine("Union: ");
        hashSet1.UnionWith(hashSet2);
        for (int i = 0; i < hashSet1.Container.Count; i++)
        {
            Console.WriteLine(hashSet1.Container.Keys[i]);
        }
        Console.WriteLine();

        Console.WriteLine("Intersect: ");
        hashSet1.IntersectWith(hashSet2);

        for (int i = 0; i < hashSet1.Container.Count; i++)
        {
            Console.WriteLine(hashSet1.Container.Keys[i]);
        }
        Console.WriteLine();

        Console.WriteLine("count after clear");
        hashSet2.Clear();
        Console.WriteLine(hashSet2.Container.Count);
    }
        public void IntersectWithCollectionsWithCommonElementsShouldReturnTheirIntersection()
        {
            testSet = new HashedSet <string>();
            testSet.Add("Pesho");
            testSet.Add("Gosho");
            testSet.Add("Kiro");

            var anotherTestSet = new HashedSet <string>();

            anotherTestSet.Add("Kiro");
            anotherTestSet.Add("Joro");
            anotherTestSet.Add("Pesho");

            var intersection = testSet.IntersectWith(anotherTestSet);

            Assert.IsTrue(
                intersection.Contains("Pesho") &&
                intersection.Contains("Kiro") &&
                2 == intersection.Count);
        }
        public void IntersectShouldReturnSetWithElementsDoubledElelemt()
        {
            var firstSet = new HashedSet<int>();
            var secondSet = new HashedSet<int>();
            for (int i = 0; i < 10; i++)
            {
                if (i < 7)
                {
                    firstSet.Add(i);
                }

                if (i > 3)
                {
                    secondSet.Add(i);
                }
            }

            var result = firstSet.IntersectWith(secondSet);

            Assert.AreEqual(3, result.Count);

            foreach (var item in result)
            {
                Assert.IsTrue(item >= 3 && item < 7);
            }
        }
        public void Intersection()
        {
            int[] oddNumbers = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25 };
            int[] digits = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            HashedSet<int> setOdds = new HashedSet<int>(oddNumbers);
            HashedSet<int> setDigits = new HashedSet<int>(digits);

            setOdds.IntersectWith(setDigits);

            int[] expectedArray = new int[] { 1, 3, 5, 7, 9 };

            int[] actualArray = setOdds.ToArray();
            Array.Sort(actualArray);
            CollectionAssert.AreEqual(expectedArray, actualArray);

            setOdds = new HashedSet<int>(oddNumbers);

            setDigits.IntersectWith(setOdds);

            actualArray = setDigits.ToArray();
            Array.Sort(actualArray);
            CollectionAssert.AreEqual(expectedArray, actualArray);

            setOdds.IntersectWith(setOdds);
            Assert.AreEqual(oddNumbers.Length, setOdds.Count);
        }
        public void IntersectShouldProducesSetContainingCommonUniqueItems()
        {
            ICollection<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8 };

            var hashset = new HashedSet<int>();

            new int[] { 6, 7, 6, 6, 8, 9, 10, 11, 12 }.ForEach(x => hashset.Add(x));

            var result = hashset.IntersectWith(numbers);

            new int[] { 6, 7, 8 }.ForEach(x => Assert.IsTrue(result.Contains(x)));

            Assert.AreEqual(3, result.Count);
        }
Example #14
0
    static void Main()
    {
        HashedSet<string> myHashSet = new HashedSet<string>();

        // Add method test
        Console.WriteLine("Add and foreach tests");
        myHashSet.Add("kir4o");
        myHashSet.Add("lili");
        myHashSet.Add("tanq");
        Console.WriteLine();
        foreach (string element in myHashSet)
        {
            Console.WriteLine(element);
        }
        Console.WriteLine();

        // Find method test
        Console.WriteLine("Find method test");
        Console.WriteLine("Find tanq = {0}", myHashSet.Find("tanq"));
        Console.WriteLine("Find sdfasdf = {0}", myHashSet.Find("sdfasdf"));
        Console.WriteLine();

        // Remove method test
        Console.WriteLine("Remove method test");
        myHashSet.Remove("lili");
        Console.WriteLine("lili removed");
        Console.WriteLine();

        // Union with test

        HashedSet<string> otherHashSet = new HashedSet<string>();
        otherHashSet.Add("venera");
        otherHashSet.Add("jupiter");
        otherHashSet.Add("kir4o");

        Console.WriteLine("Union with:");
        foreach (string element in otherHashSet)
        {
            Console.WriteLine(element);
        }
        myHashSet.UnionWith(otherHashSet);
        Console.WriteLine("Resulted hashSet:");
        foreach (string element in myHashSet)
        {
            Console.WriteLine(element);
        }
        Console.WriteLine();

        // Intersect with test
        Console.WriteLine("Intersect with :");
        foreach (string element in otherHashSet)
        {
            Console.WriteLine(element);
        }

        myHashSet.IntersectWith(otherHashSet);

        Console.WriteLine();
        Console.WriteLine("Resulted hashSet:");
        foreach (string element in myHashSet)
        {
            Console.WriteLine(element);
        }
    }