Computer Science, asked by moonlitfeathers, 10 months ago

write a program in Java to accept n numbers in array and print the array after eliminating all duplicate elements.
Sample input: n=5
numbers in array = 12123
Sample output= 123

please help me fast​

Answers

Answered by anindyaadhikari13
3

Answer:

import java.util.*;

public class RemoveDuplicateInArrayExample{

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) {

Scanner sc = new Scanner(System.in) ;

System.out.print("How many no?? ") ;

int n=sc.nextInt() ;

int arr[] = new int[n];

System.out.println(" Enter "+n+" numbers ") ;

int length = arr.length;

for(int I=0;i<length;I++)

arr[i] = sc.nextInt() ;

length = removeDuplicateElements(arr, length);

//printing array elements

for (int i=0; i<length; i++)

System.out.print(arr[i]+" ");

}

}

Explanation:

I hope this will help you..Please mark this answer as the brainliest.

10 thanks + follow = inbox ✌

Similar questions