{"id":244708,"date":"2025-07-05T09:43:34","date_gmt":"2025-07-05T09:43:34","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=244708"},"modified":"2025-07-05T09:43:36","modified_gmt":"2025-07-05T09:43:36","slug":"the-intersection-of-two-sets-is-the-set-containing-only-the-elements-common-to-both-sets","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/07\/05\/the-intersection-of-two-sets-is-the-set-containing-only-the-elements-common-to-both-sets\/","title":{"rendered":"The intersection of two sets is the set containing only the elements common to both sets"},"content":{"rendered":"\n<p>The intersection of two sets is the set containing only the elements common to both sets Write a java method that takes as parameters 2 sets and returns a LinkedHashSet containing the intersection of the two sets without changing the contents of the parameters<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-1-color\">The Correct Answer and Explanation is:<\/mark><\/strong><\/p>\n\n\n\n<p>Here\u2019s a Java method that takes two sets as parameters and returns a <code>LinkedHashSet<\/code> containing their intersection:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">javaCopyEdit<code>import java.util.Set;\nimport java.util.LinkedHashSet;\n\npublic class SetIntersection {\n    public static &lt;T&gt; LinkedHashSet&lt;T&gt; intersection(Set&lt;T&gt; set1, Set&lt;T&gt; set2) {\n        \/\/ Create a LinkedHashSet to store the intersection of both sets\n        LinkedHashSet&lt;T&gt; result = new LinkedHashSet&lt;&gt;();\n        \n        \/\/ Iterate over the first set\n        for (T element : set1) {\n            \/\/ If the element is also in the second set, add it to the result\n            if (set2.contains(element)) {\n                result.add(element);\n            }\n        }\n        \n        \/\/ Return the result, which contains only the common elements\n        return result;\n    }\n\n    public static void main(String[] args) {\n        Set&lt;Integer&gt; set1 = Set.of(1, 2, 3, 4, 5);\n        Set&lt;Integer&gt; set2 = Set.of(4, 5, 6, 7, 8);\n\n        \/\/ Calling the intersection method\n        LinkedHashSet&lt;Integer&gt; result = intersection(set1, set2);\n\n        \/\/ Printing the result\n        System.out.println(\"Intersection: \" + result); \/\/ Output: [4, 5]\n    }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Method Declaration<\/strong>:<br>The method <code>intersection<\/code> takes two sets of type <code>T<\/code> (generic type) as parameters. It returns a <code>LinkedHashSet&lt;T><\/code>, which preserves the order of elements from the first set while containing only those that are common to both sets.<\/li>\n\n\n\n<li><strong>LinkedHashSet<\/strong>:<br>A <code>LinkedHashSet<\/code> is used to store the result because it maintains the insertion order while ensuring uniqueness (i.e., no duplicates).<\/li>\n\n\n\n<li><strong>For-Each Loop<\/strong>:<br>The method iterates over the elements in <code>set1<\/code>. For each element, it checks if that element exists in <code>set2<\/code> using the <code>contains()<\/code> method.<\/li>\n\n\n\n<li><strong>Adding to Result<\/strong>:<br>If an element is found in both sets, it is added to the result set (<code>LinkedHashSet<\/code>).<\/li>\n\n\n\n<li><strong>Return the Result<\/strong>:<br>The method returns the intersection set, containing only the common elements between <code>set1<\/code> and <code>set2<\/code>.<\/li>\n\n\n\n<li><strong>Main Method<\/strong>:<br>The <code>main<\/code> method demonstrates the functionality by creating two sets, calling the <code>intersection()<\/code> method, and printing the result.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Why This Works:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>contains()<\/code> method checks if each element of <code>set1<\/code> is in <code>set2<\/code>, ensuring only common elements are added.<\/li>\n\n\n\n<li><code>LinkedHashSet<\/code> is ideal here because it preserves insertion order, which is useful when you want to maintain the sequence of elements from the original sets.<\/li>\n\n\n\n<li>This method works with any type of <code>Set<\/code> (not just <code>LinkedHashSet<\/code>) because it uses the generic type <code>T<\/code>. This allows it to be reusable for any set type (like <code>HashSet<\/code>, <code>TreeSet<\/code>, etc.).<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Efficiency:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity<\/strong>: The method runs in O(n * m) time, where <code>n<\/code> is the size of <code>set1<\/code> and <code>m<\/code> is the size of <code>set2<\/code>, since the <code>contains()<\/code> method of a set generally runs in O(1) average time.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/learnexams.com\/blog\/wp-content\/uploads\/2025\/07\/learnexams-banner5-498.jpeg\" alt=\"\" class=\"wp-image-244709\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>The intersection of two sets is the set containing only the elements common to both sets Write a java method that takes as parameters 2 sets and returns a LinkedHashSet containing the intersection of the two sets without changing the contents of the parameters The Correct Answer and Explanation is: Here\u2019s a Java method that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-244708","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/244708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=244708"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/244708\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=244708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=244708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=244708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}