{"id":11710,"date":"2021-03-06T07:01:35","date_gmt":"2021-03-06T14:01:35","guid":{"rendered":"https:\/\/sanuja.com\/blog\/?p=11710"},"modified":"2021-03-07T16:35:27","modified_gmt":"2021-03-07T23:35:27","slug":"java-remove-the-duplicate-elements","status":"publish","type":"post","link":"https:\/\/sanuja.com\/blog\/java-remove-the-duplicate-elements","title":{"rendered":"Java: remove the duplicate elements"},"content":{"rendered":"<p><b>Question:<\/b> Write a method that removes the duplicate elements from an array list of integers using the following header:<br \/><i>public static void removeDuplicate(ArrayList<Integer> list)<\/i><\/p>\n<p><!--more--><\/p>\n<p>Write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers in their input order separated by exactly one space.<\/p>\n<p><div class=\"alert alert-warning\" role=\"alert\"><p class=\"printonly\"><strong>Warning!<\/strong><\/p> Please import selected Java packages or use <em>import java.util.*;<\/em>. Do not copy my code as per copyright and plagiarism regulations. This Java code is provided as it is for demonstration purposes.<\/div><br \/>\n<b>Solution 1:<\/b><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.Scanner;\r\nimport java.util.ArrayList;\r\npublic class RemoveDuplicate {\r\n  public static void main(String&#x5B;] args) {\r\n    System.out.print(&quot;Enter ten integers: &quot;);\r\n    ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();\r\n    Scanner input = new Scanner(System.in);\r\n    for (int i = 0; i &lt; 10; i++) {\r\n      list.add(input.nextInt());\r\n    }\r\n    removeDuplicate(list);\r\n    \r\n    System.out.print(&quot;The distinct integers are &quot;);\r\n    for (int i = 0; i &lt; list.size(); i++)\r\n      System.out.print(list.get(i) + &quot; &quot;);\r\n  }\r\n  public static void removeDuplicate(ArrayList&lt;Integer&gt; list) {\r\n    ArrayList&lt;Integer&gt; temp = new ArrayList&lt;&gt;();\r\n    for (int i = 0; i &lt; list.size(); i++)\r\n      if (!temp.contains(list.get(i)))\r\n        temp.add(list.get(i));\r\n    list.clear();\r\n    for (int i = 0; i &lt; temp.size(); i++)\r\n      list.add(temp.get(i));\r\n  }\r\n}\r\n<\/pre>\n<p><b>Solution 2:<\/b><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.Scanner;\r\nimport java.util.ArrayList;\r\n\r\npublic class RemoveDuplicate {\r\n\t\/** Main method *\/\r\n\tpublic static void main(String&#x5B;] args) {\r\n\t\t\/\/ Create a scanner\r\n\t\tScanner input = new Scanner(System.in);\r\n\r\n\t\t\/\/ Create an ArrayList\r\n\t\tArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();\r\n\r\n\t\t\/\/ Prompt ther user to enter 10 integers\r\n\t\tSystem.out.print(&quot;Enter 10 integers: &quot;);\r\n\t\tfor (int i = 0; i &lt; 10; i++) {\r\n\t\t\tlist.add(input.nextInt());\r\n\t\t}\r\n\r\n\t\t\/\/ Invoke removeDuplicate method\r\n\t\tremoveDuplicate(list);\r\n\r\n\t\t\/\/ Display the distinct integers\r\n\t\tSystem.out.print(&quot;The distinct integers are &quot;);\r\n\t\tfor (int i = 0; i &lt; list.size(); i++) {\r\n\t\t\tSystem.out.print(list.get(i) + &quot; &quot;);\r\n\t\t}\r\n\t\tSystem.out.println();\r\n\t}\r\n\r\n\tpublic static void removeDuplicate(ArrayList&lt;Integer&gt; list) {\r\n\t\tfor (int i = 0; i &lt; list.size() - 1; i++) {\r\n\t\t\tfor (int j = i + 1; j &lt; list.size(); j++) {\r\n\t\t\t\tif (list.get(i) == list.get(j))\r\n\t\t\t\t\tlist.remove(j);\r\n\t\t\t}\r\n\t\t}\r\n\t\tfor (int i = 0; i &lt; list.size() - 1; i++) {\r\n\t\t\tfor (int j = i + 1; j &lt; list.size(); j++) {\r\n\t\t\t\tif (list.get(i) == list.get(j))\r\n\t\t\t\t\tlist.remove(j);\r\n\t\t\t}\r\n\t\t}\t\t\r\n\t}\r\n}\r\n<\/pre>\n<p><b>Output:<\/b><\/p>\n<p><u>Sample Run 1<\/u><br \/>\nEnter ten integers: 34 5 3 5 6 4 33 2 2 4<br \/>\nThe distinct integers are 34 5 3 6 4 33 2<\/p>\n<p><u>\ufeffSample Run 2<\/u><br \/>\nEnter ten integers: 3 3 4 4 1 1 2 2 5 5<br \/>\nThe distinct integers are 3 4 1 2 5<\/p>\n<p><u>Sample Run 3<\/u><br \/>\nEnter ten integers: 1 2 2 3 4 5 6 7 8 8<br \/>\nThe distinct integers are 1 2 3 4 5 6 7 8<\/p>\n<p><u>Sample Run 4<\/u><br \/>\nEnter ten integers: 5 4 3 2 65 4 4 5 1 5<br \/>\nThe distinct integers are 5 4 3 2 65 1<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Question: Write a method that removes the duplicate elements from an array list of integers using the following header:public static void removeDuplicate(ArrayList list)<\/p>\n","protected":false},"author":2,"featured_media":5871,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[48],"class_list":["post-11710","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-computer-science","tag-java"],"jetpack_featured_media_url":"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2013\/05\/code_black.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts\/11710","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/comments?post=11710"}],"version-history":[{"count":2,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts\/11710\/revisions"}],"predecessor-version":[{"id":11888,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts\/11710\/revisions\/11888"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/media\/5871"}],"wp:attachment":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/media?parent=11710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/categories?post=11710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/tags?post=11710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}