📜  从数组中删除重复元素的Java程序

📅  最后修改于: 2022-05-13 01:54:25.995000             🧑  作者: Mango

从数组中删除重复元素的Java程序

数组是存储在连续内存位置的项目的集合。这个想法是将多个相同类型的项目存储在一起。为简单起见,我们可以将数组视为一组楼梯,其中每一步都放置一个值

给定一个数组,任务是从数组中删除重复的元素。

例子:

Input  : a[] = {1, 1, 2, 2, 2}
Output : a[] = {1,2}
         new size = 2

Input  : a[] = {5,2,6,8,6,7,5,2,8}
Output : a[] = {2,5,6,7,8}
         new size = 5

从数组中删除重复元素的方法:

  1. 使用额外空间
  2. 恒定的额外空间
  3. 使用集合
  4. 使用频率阵列
  5. 使用哈希映射

方法一:(使用额外空间)

  1. 创建一个临时数组 temp[] 来存储唯一元素。
  2. 遍历输入数组并将 a[] 的所有唯一元素复制到 temp[]。此外,保持独特元素的数量。让这个计数为 j。
  3. 将 j 个元素从 temp[] 复制到 a[]。

注意:此方法适用于数组已排序的情况。

Java
// Java Program to Remove Duplicate Elements
// From the Array using extra space
  
public class Main {
  
    public static int removeduplicates(int a[], int n)
    {
        if (n == 0 || n == 1) {
            return n;
        }
  
        // creating another array for only storing
        // the unique elements
        int[] temp = new int[n];
        int j = 0;
  
        for (int i = 0; i < n - 1; i++) {
            if (a[i] != a[i + 1]) {
                temp[j++] = a[i];
            }
        }
  
        temp[j++] = a[n - 1];
  
        // Changing the original array
        for (int i = 0; i < j; i++) {
            a[i] = temp[i];
        }
  
        return j;
    }
    public static void main(String[] args)
    {
        int a[] = { 1, 1, 2, 2, 2 };
        int n = a.length;
  
        n = removeduplicates(a, n);
  
        // Printing The array elements
        for (int i = 0; i < n; i++)
            System.out.print(a[i] + " ");
    }
}


Java
// Java Program to Remove Duplicate Elements
// From the Array using extra space
  
public class Main {
    public static int removeDuplicates(int a[], int n)
    {
        // if(array size if 0 or 1 array is already sorted)
        if (n == 0 || n == 1) {
            return n;
        }
  
        int j = 0;
  
        // check if the ith element is not equal to 
        // the (i+1)th element, then add that element
        // at the jth index in the same array
        // which indicates that te particular element 
        // will only be added once in the array
        for (int i = 0; i < n - 1; i++) {
            if (a[i] != a[i + 1]) {
                a[j++] = a[i];
            }
        }
  
        a[j++] = a[n - 1];
  
        return j;
    }
  
    public static void main(String[] args)
    {
        int a[] = { 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 6 };
  
        int n = a.length;
         
        int j=0;
        
        // the function will modify the array a[]
        // such that the starting j elements 
        // will be having all unique elements 
        // and no element will be appearing more than
        // once
        j = removeDuplicates(a, n);
  
        // printing array elements
        for (int i = 0; i < j; i++)
            System.out.print(a[i] + " ");
    }
}


Java
// Java Program to Remove Duplicate Elements
// From the Array using Set
  
import java.util.*;
  
class GFG {
  
    // Function to remove duplicate from array
    public static void removeDuplicates(int[] a)
    {
        LinkedHashSet set
            = new LinkedHashSet();
  
        // adding elements to LinkedHashSet
        for (int i = 0; i < a.length; i++)
            set.add(a[i]);
  
        // Print the elements of LinkedHashSet
        System.out.print(set);
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int a[] = {5,2,6,8,6,7,5,2,8};
      
        // Function call
        removeDuplicates(a);
    }
}


Java
// Java Program to Remove Duplicate Elements
// From the Array by maintaining frequency array
  
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        int a[] = { 5, 2, 6, 8, 6, 7, 5, 2, 8 };
        int n = a.length;
  
        // m will have the maximum element in the array.
        int m = 0;
  
        for (int i = 0; i < n; i++) {
            m = Math.max(m, a[i]);
        }
  
        // creating the frequency array
        int[] f = new int[m + 1];
  
        // initializing the f[] with 0
        for (int i = 0; i < m + 1; i++) {
            f[i] = 0;
        }
  
        // incrementing the value at a[i]th index
        // in the frequency array
        for (int i = 0; i < n; i++) 
        {
            f[a[i]]++;
        }
  
        for (int i = 0; i < m + 1; i++) 
        {
    
            // if the frequency of the particular element
            // is greater than 0, then print it once
            if (f[i] > 0) {
                System.out.print(i + " ");
            }
        }
    }
}


Java
// Java Program to Remove Duplicate Elements
// From the Array using HashMap
  
import java.util.HashMap;
  
class GFG {
    static void removeDups(int[] a, int n)
    {
  
        // Hash map which will store the
        // elements which has appeared previously.
        HashMap mp = new HashMap<>();
  
        for (int i = 0; i < n; ++i) {
  
            // Print the element if it is not
            // present there in the hash map
            // and Insert the element in the hash map
            if (mp.get(a[i]) == null)
            {
                System.out.print(a[i] + " ");
                mp.put(a[i], true);
            }
        }
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 1, 2, 5, 1, 7, 2, 4, 2 };
        
        int n = arr.length;
        removeDups(arr, n);
    }
}


输出
1 2

时间复杂度:O(n)

空间复杂度: O(n)

方法 2 :(恒定的额外空间)

只需为方法 1 中为不同数组维护的同一数组维护一个单独的索引。

Java

// Java Program to Remove Duplicate Elements
// From the Array using extra space
  
public class Main {
    public static int removeDuplicates(int a[], int n)
    {
        // if(array size if 0 or 1 array is already sorted)
        if (n == 0 || n == 1) {
            return n;
        }
  
        int j = 0;
  
        // check if the ith element is not equal to 
        // the (i+1)th element, then add that element
        // at the jth index in the same array
        // which indicates that te particular element 
        // will only be added once in the array
        for (int i = 0; i < n - 1; i++) {
            if (a[i] != a[i + 1]) {
                a[j++] = a[i];
            }
        }
  
        a[j++] = a[n - 1];
  
        return j;
    }
  
    public static void main(String[] args)
    {
        int a[] = { 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 6 };
  
        int n = a.length;
         
        int j=0;
        
        // the function will modify the array a[]
        // such that the starting j elements 
        // will be having all unique elements 
        // and no element will be appearing more than
        // once
        j = removeDuplicates(a, n);
  
        // printing array elements
        for (int i = 0; i < j; i++)
            System.out.print(a[i] + " ");
    }
}
输出
1 2 3 4 5 6

时间复杂度:O(n)

辅助空间:O(1)

方法 3使用 Set

即使数组未排序,也可以使用此方法。

方法:

  1. 拿一套
  2. 在 Set 中插入所有数组元素。 Set 不允许重复,而像 LinkedHashSet 这样的 Set 会维护插入顺序,因此它将删除重复项,并且元素将按照插入的顺序打印。
  3. 打印 Set 的元素。

Java

// Java Program to Remove Duplicate Elements
// From the Array using Set
  
import java.util.*;
  
class GFG {
  
    // Function to remove duplicate from array
    public static void removeDuplicates(int[] a)
    {
        LinkedHashSet set
            = new LinkedHashSet();
  
        // adding elements to LinkedHashSet
        for (int i = 0; i < a.length; i++)
            set.add(a[i]);
  
        // Print the elements of LinkedHashSet
        System.out.print(set);
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int a[] = {5,2,6,8,6,7,5,2,8};
      
        // Function call
        removeDuplicates(a);
    }
}
输出
[5, 2, 6, 8, 7]

方法四:使用频率数组

如果数组中的数字范围有限,我们可以使用频率数组,或者如果数组中的数字范围太大,我们也可以使用 set 或 map 接口去除重复项。

方法:

  1. 查找数组中的最大元素 (m)。
  2. 创建一个大小为 m+1 的新数组。
  3. 现在遍历输入数组并计算输入数组中每个元素的频率。
  4. 现在遍历频率数组并检查每个数字的频率,如果特定元素的频率大于 0,则打印该数字。

Java

// Java Program to Remove Duplicate Elements
// From the Array by maintaining frequency array
  
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        int a[] = { 5, 2, 6, 8, 6, 7, 5, 2, 8 };
        int n = a.length;
  
        // m will have the maximum element in the array.
        int m = 0;
  
        for (int i = 0; i < n; i++) {
            m = Math.max(m, a[i]);
        }
  
        // creating the frequency array
        int[] f = new int[m + 1];
  
        // initializing the f[] with 0
        for (int i = 0; i < m + 1; i++) {
            f[i] = 0;
        }
  
        // incrementing the value at a[i]th index
        // in the frequency array
        for (int i = 0; i < n; i++) 
        {
            f[a[i]]++;
        }
  
        for (int i = 0; i < m + 1; i++) 
        {
    
            // if the frequency of the particular element
            // is greater than 0, then print it once
            if (f[i] > 0) {
                System.out.print(i + " ");
            }
        }
    }
}
输出
2 5 6 7 8

时间复杂度:O(n)

辅助空间:O(m)

方法五:使用HashMap

如果数字大于 10 6或者数组是字符串,上述频率方法将没有用。在这种情况下,我们必须使用 HashMap。

方法:

  • 创建一个 HashMap 来存储唯一元素。
  • 遍历数组。
  • 检查元素是否存在于 HashMap 中。
  • 如果是,继续遍历数组。
  • Else 打印元素并将元素存储在 HashMap 中。

Java

// Java Program to Remove Duplicate Elements
// From the Array using HashMap
  
import java.util.HashMap;
  
class GFG {
    static void removeDups(int[] a, int n)
    {
  
        // Hash map which will store the
        // elements which has appeared previously.
        HashMap mp = new HashMap<>();
  
        for (int i = 0; i < n; ++i) {
  
            // Print the element if it is not
            // present there in the hash map
            // and Insert the element in the hash map
            if (mp.get(a[i]) == null)
            {
                System.out.print(a[i] + " ");
                mp.put(a[i], true);
            }
        }
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 1, 2, 5, 1, 7, 2, 4, 2 };
        
        int n = arr.length;
        removeDups(arr, n);
    }
}
输出
1 2 5 7 4

时间复杂度:O(n)

辅助空间:O(m)