Java program to remove duplicate elements from an array

Other Blogs

Blogs ❯❯ Java

Image could not load

Remove duplicates from an array using ArrayList

ArrayList का use करके duplicate elements को remove करने के लिए सबसे पहले सभी elements को एक new ArrayList में insert करेंगे , फिर उस list को iterate करके new list में insert करते रहेंगे। new list में insert करने से पहले contains() method की help से हम check करेंगे कि element exist है या नहीं।

Java program to remove duplicate elements from array

public class RemoveDuplicates { public static void main(String[] args) { // Array with duplicate elements int[] arr = {1, 2, 3, 4, 2, 7, 8, 8, 3, 1}; // Create a list from the array ArrayList<Integer> al = new ArrayList<>(); for (int i : arr) al.add(i); // Remove duplicates ArrayList<Integer> newList = new ArrayList<>(); for (Integer element : al) { if (!newList.contains(element)) { newList.add(element); } } // Finally iterate the list to print element. for (Integer element : newList) { System.out.print(element + " "); } } }
Recent Blogs

Loading ...

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 4.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook

Your Thought ?

Please wait . . .

    0 Comment(s) found !