{"id":66565,"date":"2025-05-03T11:23:22","date_gmt":"2025-05-03T03:23:22","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66565\/"},"modified":"2025-05-03T11:23:22","modified_gmt":"2025-05-03T03:23:22","slug":"java%e6%95%b0%e7%bb%84%e9%87%8c%e6%80%8e%e4%b9%88%e5%8e%bb%e9%99%a4%e7%9b%b8%e4%bc%bc%e7%9a%84%e5%85%83%e7%b4%a0-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66565\/","title":{"rendered":"Java\u6570\u7ec4\u91cc\u600e\u4e48\u53bb\u9664\u76f8\u4f3c\u7684\u5143\u7d20"},"content":{"rendered":"<blockquote><p>\n  java \u6570\u7ec4\u53bb\u9664\u76f8\u4f3c\u5143\u7d20\u7684\u65b9\u6cd5\uff1a\u521b\u5efa hashset\uff0c\u5b58\u50a8\u6570\u7ec4\u4e2d\u5143\u7d20\uff0c\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u9879\u3002\u904d\u5386 hashset\uff0c\u5c06\u5143\u7d20\u5b58\u50a8\u5230\u65b0\u6570\u7ec4\u4e2d\u3002\u4ee3\u7801\u793a\u4f8b\uff1aimport java.util.arrays;import java.util.hashset;int[] arr = {1, 2, 3, 4, 5, 1, 2, 3};hashset set = new hashset(arrays.aslist(arr));int[] uniquearr = new in\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111606305990885.jpg\" class=\"aligncenter\" title=\"Java\u6570\u7ec4\u91cc\u600e\u4e48\u53bb\u9664\u76f8\u4f3c\u7684\u5143\u7d20\u63d2\u56fe\" alt=\"Java\u6570\u7ec4\u91cc\u600e\u4e48\u53bb\u9664\u76f8\u4f3c\u7684\u5143\u7d20\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u6570\u7ec4\u4e2d\u53bb\u9664\u76f8\u4f3c\u5143\u7d20<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>Java \u6570\u7ec4\u4e2d\u53bb\u9664\u76f8\u4f3c\u5143\u7d20\u662f\u6307\u5220\u9664\u6570\u7ec4\u4e2d\u6240\u6709\u91cd\u590d\u7684\u5143\u7d20\uff0c\u5f97\u5230\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u5176\u4e2d\u53ea\u5305\u542b\u552f\u4e00\u503c\u3002<\/p>\n<p><strong>\u65b9\u6cd5<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4ee5\u4e0b\u662f\u5982\u4f55\u5728 Java \u6570\u7ec4\u4e2d\u53bb\u9664\u76f8\u4f3c\u5143\u7d20\u7684\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li> <strong>\u521b\u5efa HashSet\uff1a<\/strong>\u7528\u6570\u7ec4\u4e2d\u5143\u7d20\u521b\u5efa HashSet\uff0c\u5b83\u53ea\u5141\u8bb8\u552f\u4e00\u5143\u7d20\u3002<\/li>\n<li> <strong>\u8fed\u4ee3 HashSet\uff1a<\/strong>\u904d\u5386 HashSet \u5e76\u5c06\u5143\u7d20\u5b58\u50a8\u5728\u65b0\u7684\u6570\u7ec4\u4e2d\u3002<\/li>\n<\/ol>\n<p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n<pre>import java.util.Arrays;\nimport java.util.HashSet;\n\npublic class RemoveDuplicates {\n\n    public static void main(String[] args) {\n        \/\/ \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u91cd\u590d\u5143\u7d20\u7684\u6570\u7ec4\n        int[] arr = {1, 2, 3, 4, 5, 1, 2, 3};\n\n        \/\/ \u521b\u5efa HashSet \u4ee5\u5220\u9664\u91cd\u590d\u5143\u7d20\n        HashSet&lt;Integer&gt; set = new HashSet&lt;&gt;();\n        for (int i : arr) {\n            set.add(i);\n        }\n\n        \/\/ \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\u6765\u5b58\u50a8\u552f\u4e00\u503c\n        int[] uniqueArr = new int[set.size()];\n        int index = 0;\n        for (int val : set) {\n            uniqueArr[index++] = val;\n        }\n\n        \/\/ \u6253\u5370\u552f\u4e00\u503c\u7684\u6570\u7ec4\n        System.out.println(Arrays.toString(uniqueArr));\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa<\/strong><\/p>\n<pre>[1, 2, 3, 4, 5]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f<\/strong><\/p>\n<ul>\n<li>\u4e5f\u53ef\u4ee5\u4f7f\u7528 <strong>LinkedHashSet<\/strong> \u800c\u4e0d\u662f <strong>HashSet<\/strong>\uff0c\u4ee5\u4fdd\u7559\u5143\u7d20\u7684\u539f\u59cb\u987a\u5e8f\u3002<\/li>\n<li>\u8fd9\u79cd\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\uff0c\u5176\u4e2d n \u662f\u6570\u7ec4\u7684\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fJava\u6570\u7ec4\u91cc\u600e\u4e48\u53bb\u9664\u76f8\u4f3c\u7684\u5143\u7d20\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>java \u6570\u7ec4\u53bb\u9664\u76f8\u4f3c\u5143\u7d20\u7684\u65b9\u6cd5\uff1a\u521b\u5efa hashset\uff0c\u5b58\u50a8\u6570\u7ec4\u4e2d\u5143\u7d20\uff0c\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u9879\u3002\u904d\u5386 hashset\uff0c\u5c06\u5143\u7d20\u5b58\u50a8\u5230\u65b0\u6570\u7ec4\u4e2d\u3002\u4ee3\u7801\u793a\u4f8b\uff1aimport java.util.arrays;import java.util.hashset;int[] arr = {1, 2, 3, 4, 5, 1, 2, 3};hashset set = new hashset(arrays.aslist(arr));int[] uniquearr = new in Java \u6570\u7ec4\u4e2d\u53bb\u9664\u76f8\u4f3c\u5143\u7d20 \u7b80\u4ecb Java \u6570\u7ec4\u4e2d\u53bb\u9664\u76f8\u4f3c\u5143\u7d20\u662f\u6307\u5220\u9664\u6570\u7ec4\u4e2d\u6240\u6709\u91cd\u590d\u7684\u5143\u7d20\uff0c\u5f97\u5230\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u5176\u4e2d\u53ea\u5305\u542b\u552f\u4e00\u503c\u3002 \u65b9\u6cd5 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4ee5\u4e0b\u662f\u5982\u4f55\u5728 Java \u6570\u7ec4\u4e2d\u53bb\u9664\u76f8\u4f3c\u5143\u7d20\u7684\u6b65\u9aa4\uff1a \u521b\u5efa HashSet\uff1a\u7528\u6570\u7ec4\u4e2d\u5143\u7d20\u521b\u5efa HashSet\uff0c\u5b83\u53ea\u5141\u8bb8\u552f\u4e00\u5143\u7d20\u3002 \u8fed\u4ee3 HashSet\uff1a\u904d\u5386 HashSet \u5e76\u5c06\u5143\u7d20\u5b58\u50a8\u5728\u65b0\u7684\u6570\u7ec4\u4e2d\u3002 \u4ee3\u7801\u793a\u4f8b import java.util.Arrays; import java.util.HashSet; public class RemoveDuplicates { public static void main(String[] args) [&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-66565","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66565","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=66565"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66565\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}