{"id":66760,"date":"2025-05-03T17:29:55","date_gmt":"2025-05-03T09:29:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66760\/"},"modified":"2025-05-03T17:29:55","modified_gmt":"2025-05-03T09:29:55","slug":"java%e6%80%8e%e4%b9%88%e6%89%be%e5%87%ba%e6%95%b0%e7%bb%84%e5%94%af%e4%b8%80%e4%b8%80%e4%b8%aa-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66760\/","title":{"rendered":"java\u600e\u4e48\u627e\u51fa\u6570\u7ec4\u552f\u4e00\u4e00\u4e2a"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u627e\u51fa\u6570\u7ec4\u4e2d\u552f\u4e00\u7684\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a\u4f7f\u7528 hashset\uff1a\u521b\u5efa\u4e00\u4e2a hashset\uff0c\u904d\u5386\u6570\u7ec4\u5e76\u5c06\u5143\u7d20\u6dfb\u52a0\u5230 hashset \u4e2d\u3002hashset \u4e2d\u5269\u4f59\u7684\u5143\u7d20\u5373\u4e3a\u552f\u4e00\u7684\u5143\u7d20\u3002\u4f7f\u7528 hashmap\uff1a\u521b\u5efa\u4e00\u4e2a hashmap\uff0c\u952e\u4e3a\u6570\u7ec4\u5143\u7d20\uff0c\u503c\u4e3a\u51fa\u73b0\u6b21\u6570\u3002\u904d\u5386\u6570\u7ec4\u66f4\u65b0\u8ba1\u6570\u3002\u503c\u4e3a 1 \u7684\u952e\u8868\u793a\u552f\u4e00\u7684\u5143\u7d20\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111622490486171.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u627e\u51fa\u6570\u7ec4\u552f\u4e00\u4e00\u4e2a\u63d2\u56fe\" alt=\"java\u600e\u4e48\u627e\u51fa\u6570\u7ec4\u552f\u4e00\u4e00\u4e2a\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u627e\u51fa\u6570\u7ec4\u4e2d\u552f\u4e00\u7684\u5143\u7d20<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u627e\u51fa\u6570\u7ec4\u4e2d\u552f\u4e00\u7684\u5143\u7d20\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p><strong>\u4f7f\u7528 HashSet<\/strong><\/p>\n<p>HashSet \u662f\u4e00\u79cd\u96c6\u5408\u7c7b\u578b\uff0c\u5b83\u4e0d\u4f1a\u5b58\u50a8\u91cd\u590d\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a HashSet uniqueElements\u3002<\/li>\n<li>\u904d\u5386\u6570\u7ec4\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5143\u7d20\u6dfb\u52a0\u5230 uniqueElements \u4e2d\u3002<\/li>\n<li>\u904d\u5386 uniqueElements \u4e2d\u7684\u5143\u7d20\u5e76\u5c06\u5176\u8f93\u51fa\u6216\u5b58\u50a8\u5728\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u4e2d\u3002<\/li>\n<\/ol>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>import java.util.HashSet;\nimport java.util.Set;\n\npublic class FindUniqueElements {\n\n    public static void main(String[] args) {\n        int[] arr = {1, 2, 3, 4, 5, 1, 2};\n\n        \/\/ \u521b\u5efa HashSet\n        Set&lt;Integer&gt; uniqueElements = new HashSet&lt;&gt;();\n\n        \/\/ \u904d\u5386\u6570\u7ec4\u5e76\u6dfb\u52a0\u5230 HashSet\n        for (int i : arr) {\n            uniqueElements.add(i);\n        }\n\n        \/\/ \u6253\u5370\u552f\u4e00\u5143\u7d20\n        for (int element : uniqueElements) {\n            System.out.print(element + \" \");\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>3 4 5<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a<\/strong>O(n)<\/p>\n<p><strong>\u4f7f\u7528 HashMap<\/strong><\/p>\n<p>HashMap \u4e5f\u662f\u4e00\u79cd\u96c6\u5408\u7c7b\u578b\uff0c\u5b83\u5c06\u5b58\u50a8\u5728\u4e00\u8d77\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a HashMap elementCounts\uff0c\u5176\u4e2d\u952e\u4e3a\u6570\u7ec4\u5143\u7d20\uff0c\u503c\u4e3a\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/li>\n<li>\u904d\u5386\u6570\u7ec4\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u5143\u7d20\u66f4\u65b0\u5176\u8ba1\u6570\u3002<\/li>\n<li>\u904d\u5386 elementCounts \u4e2d\u7684\u952e\uff0c\u5e76\u627e\u51fa\u503c\u7b49\u4e8e 1 \u7684\u952e\uff08\u8868\u793a\u552f\u4e00\u7684\u5143\u7d20\uff09\u3002<\/li>\n<\/ol>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>import java.util.HashMap;\nimport java.util.Map;\n\npublic class FindUniqueElements {\n\n    public static void main(String[] args) {\n        int[] arr = {1, 2, 3, 4, 5, 1, 2};\n\n        \/\/ \u521b\u5efa HashMap\n        Map&lt;Integer, Integer&gt; elementCounts = new HashMap&lt;&gt;();\n\n        \/\/ \u904d\u5386\u6570\u7ec4\u5e76\u66f4\u65b0\u8ba1\u6570\n        for (int i : arr) {\n            int count = elementCounts.getOrDefault(i, 0);\n            elementCounts.put(i, count + 1);\n        }\n\n        \/\/ \u627e\u5230\u552f\u4e00\u7684\u5143\u7d20\n        for (Map.Entry&lt;Integer, Integer&gt; entry : elementCounts.entrySet()) {\n            if (entry.getValue() == 1) {\n                System.out.println(entry.getKey());\n            }\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>3\n4\n5<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a<\/strong>O(n)<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u627e\u51fa\u6570\u7ec4\u552f\u4e00\u4e00\u4e2a\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 java \u4e2d\u627e\u51fa\u6570\u7ec4\u4e2d\u552f\u4e00\u7684\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a\u4f7f\u7528 hashset\uff1a\u521b\u5efa\u4e00\u4e2a hashset\uff0c\u904d\u5386\u6570\u7ec4\u5e76\u5c06\u5143\u7d20\u6dfb\u52a0\u5230 hashset \u4e2d\u3002hashset \u4e2d\u5269\u4f59\u7684\u5143\u7d20\u5373\u4e3a\u552f\u4e00\u7684\u5143\u7d20\u3002\u4f7f\u7528 hashmap\uff1a\u521b\u5efa\u4e00\u4e2a hashmap\uff0c\u952e\u4e3a\u6570\u7ec4\u5143\u7d20\uff0c\u503c\u4e3a\u51fa\u73b0\u6b21\u6570\u3002\u904d\u5386\u6570\u7ec4\u66f4\u65b0\u8ba1\u6570\u3002\u503c\u4e3a 1 \u7684\u952e\u8868\u793a\u552f\u4e00\u7684\u5143\u7d20\u3002 \u5982\u4f55\u627e\u51fa\u6570\u7ec4\u4e2d\u552f\u4e00\u7684\u5143\u7d20 \u5728 Java \u4e2d\uff0c\u627e\u51fa\u6570\u7ec4\u4e2d\u552f\u4e00\u7684\u5143\u7d20\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a \u4f7f\u7528 HashSet HashSet \u662f\u4e00\u79cd\u96c6\u5408\u7c7b\u578b\uff0c\u5b83\u4e0d\u4f1a\u5b58\u50a8\u91cd\u590d\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u521b\u5efa\u4e00\u4e2a HashSet uniqueElements\u3002 \u904d\u5386\u6570\u7ec4\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5143\u7d20\u6dfb\u52a0\u5230 uniqueElements \u4e2d\u3002 \u904d\u5386 uniqueElements \u4e2d\u7684\u5143\u7d20\u5e76\u5c06\u5176\u8f93\u51fa\u6216\u5b58\u50a8\u5728\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u4e2d\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a import java.util.HashSet; import java.util.Set; public class FindUniqueElements { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5, 1, 2}; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-66760","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66760","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=66760"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66760\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}