how to remove duplicates from array in java without using collections?

Today, We want to share with you how to remove duplicates from array in java without using collections.In this post we will show you how to remove duplicates from an array java?, hear for how to remove duplicate elements from arraylist in java without using collections? we will give you demo and example for implement.In this post, we will learn about Ramda Remove Duplicates with an example.

Read Also:  Advanced Pagination with PHP, MySQL and jQuery Demo

remove duplicates from array without using collection

Example 1:

public class RemoveDuplicateInArrayExample{  
public static int removeDuplicateElements(int products[], int n){  
        if (n==0 || n==1){  
            return n;  
        }  
        int[] product = new int[n];  
        int j = 0;  
        for (int i=0; i<n-1; i++){  
            if (products[i] != products[i+1]){  
                product[j++] = products[i];  
            }  
         }  
        product[j++] = products[n-1];     
        // Changing original array  
        for (int i=0; i<j; i++){  
            products[i] = product[i];  
        }  
        return j;  
    }  
       
    public static void main (String[] args) {  
        int products[] = {10,20,20,30,30,40,50,50};  
        int length = products.length;  
        length = removeDuplicateElements(products, length);  
        //displaying array elements  
        for (int i=0; i<length; i++)  
           System.out.print(products[i]+" ");  
    }  
} 

Remove duplicates from arraylist without using collections

Write a simple Java program to remove duplicate elements from an arraylist without using collections (without using set)

package arrayListRemoveduplicateElements;
import java.util.ArrayList;
 
public class RemoveDuplicates {
public static void main(String[] args){
    
    ArrayList<Object> website = new ArrayList<Object>();
    
    website.add("pakainfo");
    website.add('a');
    website.add('b');
    website.add('a');
    website.add("pakainfo");
    website.add(10.3);
    website.add('c');
    website.add(14);
    website.add("pakainfo");
    website.add(12);
    
System.out.println("Before Remove Duplicate website:"+website);
 
for(int i=0;i<website.size();i++){
 
 for(int j=i+1;j<website.size();j++){
            if(website.get(i).equals(website.get(j))){
                website.remove(j);
                j--;
            }
    }
 
 }
 
    System.out.println("After Removing some duplicate website:"+website);
 
}
 
}

Remove duplicates in array using LinkedHashSet

RemoveDuplicates.java

import java.util.Arrays;
import java.util.LinkedHashSet;
 
public class RemoveDuplicates 
{
    public static void main(String[] args) throws CloneNotSupportedException 
    {
        //Array with duplicate website
        Integer[] website = new Integer[] {1,2,3,4,5,1,3,5};
         
        //This Java array has duplicate website
        System.out.println( Arrays.toString(website) );
         
        //Make set from array website
        LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>( Arrays.asList(website) );
         
        //Get back the array without duplicates
        Integer[] numbersWithoutDuplicates = linkedHashSet.toArray(new Integer[] {});
         
        //Confirm the array data content
        System.out.println( Arrays.toString(numbersWithoutDuplicates) );
    }
}

I hope you get an idea about remove duplicate items from array.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  Laravel Logging an Array Example

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  AngularJs Clone Array
  2. Read Also:  PHP Laravel 5.7 Get IP Address Example
  3. Read Also:  Remove Special Characters from String using C#
  4. Read Also:  Laravel 6 CRUD Tutorial With Example from scratch
  5. Read Also:  How to Integrate Google No CAPTCHA reCAPTCHA using VueJS
  6. Read Also:  Laravel Bootstrap Toggle switch Update DB field using Ajax
  7. Read Also:  ng-table Sorting using Angular Example
  8. Read Also:  How to Send Twitter Automatically Tweets with PHP
  9. Read Also:  How to Integrate Google No CAPTCHA reCAPTCHA using VueJS
  10. Read Also:  AngularJs Clone Array
CLOSEX