An array is an information structure that contains a gathering of components. Commonly these components are the greater part of similar information compose, for example, a whole number or string. Arrays are regularly utilised as a part of PC projects to compose information with the goal that a related arrangement of qualities can be effectively arranged or searched.In java Program to expel copy component in an Array
We can expel copy component in an array by 2 different ways: utilising impermanent array or utilising separate record. To expel the copy component from array, the array must be in arranged request. On the off chance that array isn't arranged, you can sort it by calling Arrays sort strategy.
1) Remove Duplicate Element in Array using Temporary Array
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package prime;
/**
*
* @author rajesh kumar shukla
*/
public class RemoveDuplicateArrayExample1{
public static int RemoveDuplicateArrayExample1(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[] = {10,20,20,30,30,40,50,50};
int length = arr.length;
length = RemoveDuplicateArrayExample1(arr, length);
//printing array elements
for (int i=0; i<length; i++)
System.out.print(arr[i]+" ");
}
}
2) Remove Duplicate Element in Array using separate index
/** To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package prime;
/**
*
* @author rajesh kumar shukla
*/
public class RemoveDuplicateArrayExample2{
public static int removeDuplicateElements(int arr[], int n){
if (n==0 || n==1){
return n;
}
int j = 0;//for next element
for (int i=0; i < n-1; i++){
if (arr[i] != arr[i+1]){
arr[j++] = arr[i];
}
}
arr[j++] = arr[n-1];
return j;
}
public static void main (String[] args) {
int arr[] = {10,20,20,30,30,40,50,50};
int length = arr.length;
length = removeDuplicateElements(arr, length);
//printing array elements
for (int i=0; i<length; i++)
System.out.print(arr[i]+" ");
}
}
3)Remove Duplicate Elements in Unsorted Array
If you have unsorted array, you need to sort it first. To do so, use Arrays.sort(arr) method.import java.util.Arrays;
public class RemoveDuplicateInArrayExample3{
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[] = {10,70,30,90,20,20,30,40,70,50};//unsorted array
Arrays.sort(arr);//sorting array
int length = arr.length;
length = removeDuplicateElements(arr, length);
//printing array elements
for (int i=0; i<length; i++)
System.out.print(arr[i]+" ");
}
}
4)Remove Duplicate
/** To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package prime;
/**
*
* @author rajesh kumar shukla
*/
public class RemoveDuplicateElements
{
public static int[] removeDuplicates(int[] input)
{
int j = 0;
int i = 1;
// return if the array length is less than 2
if (input.length < 2)
{
return input;
}
while (i < input.length)
{
if (input[i] == input[j])
{
i++;
}
else
{
input[++j] = input[i++];
}
}
int[] output = new int[j + 1];
for (int k = 0; k < output.length; k++)
{
output[k] = input[k];
}
return output;
}
public static void main(String a[])
{
int[] input1 = { 2, 3, 6, 6, 8, 9, 10, 10, 10, 12, 12 };
int[] output = removeDuplicates(input1);
System.out.print("Input Elements: \n");
for (int i : input1)
{
System.out.print(i + " ");
}
System.out.print("\nOutput Elements: \n");
for (int i : output)
{
System.out.print(i + " ");
}
}
}
Output
Input Elements:
2 3 6 8 9 10 12 10 10 12 12
Output Elements:
2 3 6 8 9 10 12
0 comments:
Post a Comment