>Write a Program to remove duplicate element in an array

Write a Program to remove duplicate element in an array

Write a Program to remove duplicate element in an array

Program

public class Main 
{
    public static int removeDuplicateElements(int arr[], int n)
    {  
        if (n==0 || n==1)
        {  
            return n;  
        }  
        int[] temp = new int[n];  
        int j = 0;  
        for (int i=0; i<n-1; i++)
        {  
            if (arr[i] != arr[i+1])
            {  
                temp[j++] = arr[i];  
            }  
        }  
        temp[j++] = arr[n-1];     
        // Changing original array  
        for (int i=0; i<j; i++)
        {  
            arr[i] = temp[i];  
        }  
        return j;  
    }  
    public static void main (String[] args)
    {  
        int arr[] = {100,200,200,300,300,40,50,50};  
        int length = arr.length;  
        length = removeDuplicateElements(arr, length);  
        for (int i=0; i<length; i++)  
        {
            System.out.print(arr[i]+" "); 
        }
    }
}


Output:

100 200 300 40 50



More Questions


43 . Write a java program to Find the index of the smallest number in an array
44 . Write a Program to remove duplicate element in an array
45 . Write a java Program to print odd and even numbers from an array
46 . Write a java program to add two matrix
47 . Write a java Program to check given matrix is null matrix
48 . Add all the digits of a given positive integer until the result has a single digit
49 . Write a Java program to accept a float value of number and return a rounded float value



For latest job updates join Telegram Channel: https://t.me/sateeshm

Apply For The Job

Apply For Job

Java Material

Python Material

PLSQL Material

.Net Material

Java Practice Programs

400 Java Programs