46. JAVA Program to Remove Duplicate or Repeated Number.
How does this program work?
- In this program your going to learn about how to remove repeated number from the given array java.
Here is the code
// Remove Duplicate OR Repeated Number in java
import java.util.Scanner;public class Array
{
public static void main(String[] args )
{
int initial_element,next_element,i;
int[] arr_elements = new int[20];
/*Display array size for user*/
System.out.print("Enter array size: ");Scanner sc = new Scanner(System.in);
int arr_size = sc.nextInt();
// System.out.println("Read Array Elements From User:");
/*Loop to take input array elements*/
for(i=0;i< arr_size;++i){
//System.out.print("Enter array elements of index " +i +": \n");
arr_elements[i] = sc.nextInt();}
/* Display array before removing duplicate element */
for(i=0;i< arr_size;++i){
System.out.println(arr_elements[i]);
}
System.out.println("After removing duplicate element array are:\n");
for(initial_element=0;initial_element< arr_size;++initial_element)
{
for(next_element=initial_element+1;next_element< arr_size;)
{
if(arr_elements[initial_element] == arr_elements[next_element])
{
for(int temp = next_element; temp< arr_size; ++temp)
{
arr_elements[temp] = arr_elements[temp+1];
}
arr_size = arr_size-1;
}
else
next_element++;
}
}
/* Loop to display array after removing duplicate element */
for(i=0;i< arr_size;++i)System.out.println(arr_elements[i]);
}
}