📜  从排序数组中删除重复项

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

从排序数组中删除重复项

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

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

Input  : arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5}
Output : arr[] = {1, 2, 3, 4, 5}
         new size = 5

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

  1. 创建一个辅助数组 temp[] 来存储唯一元素。
  2. 遍历输入数组,并将 arr[] 的唯一元素一一复制到 temp[]。还要跟踪独特元素的数量。设此计数为j
  3. j 个元素从 temp[] 复制到 arr[] 并返回 j

C++
// Simple C++ program to remove duplicates
#include
using namespace std;
 
// Function to remove duplicate elements
// This function returns new size of modified
// array.
int removeDuplicates(int arr[], int n)
{
    // Return, if array is empty
    // or contains a single element
    if (n==0 || n==1)
        return n;
 
    int temp[n];
 
    // Start traversing elements
    int j = 0;
    for (int i=0; i


Java
// simple java program to remove
// duplicates
 
class Main
{
    // Function to remove duplicate elements
    // This function returns new size of modified
    // array.
    static int removeDuplicates(int arr[], int n)
    {
        // Return, if array is empty
        // or contains a single element
        if (n==0 || n==1)
            return n;
      
        int[] temp = new int[n];
         
        // Start traversing elements
        int j = 0;
        for (int i=0; i


Python3
# Python3 program to
# remove duplicates
# Function to remove
# duplicate elements
 
# This function returns
# new size of modified
# array.
def removeDuplicates(arr, n):
 
    # Return, if array is
    # empty or contains
    # a single element
    if n == 0 or n == 1:
        return n
 
    temp = list(range(n))
 
    # Start traversing elements
    j = 0;
    for i in range(0, n-1):
 
        # If current element is
        # not equal to next
        # element then store that
        # current element
        if arr[i] != arr[i+1]:
            temp[j] = arr[i]
            j += 1
 
    # Store the last element
    # as whether it is unique
    # or repeated, it hasn't
    # stored previously
    temp[j] = arr[n-1]
    j += 1
     
    # Modify original array
    for i in range(0, j):
        arr[i] = temp[i]
 
    return j
 
# Driver code
arr = [1, 2, 2, 3, 4, 4, 4, 5, 5]
n = len(arr)
 
# removeDuplicates() returns
# new size of array.
n = removeDuplicates(arr, n)
 
# Print updated array
for i in range(n):
    print ("%d"%(arr[i]), end = " ")


C#
// Simple C# program to remove
// duplicates
using System;
 
class GFG {
     
    // Function to remove duplicate
    // elements This function returns
    // new size of modified array.
    static int removeDuplicates(int []arr, int n)
    {
         
        // Return, if array is empty
        // or contains a single element
        if (n == 0 || n == 1)
            return n;
     
        int []temp = new int[n];
         
        // Start traversing elements
        int j = 0;
         
        for (int i = 0; i < n - 1; i++)
         
            // If current element is not equal
            // to next element then store that
            // current element
            if (arr[i] != arr[i+1])
                temp[j++] = arr[i];
         
        // Store the last element as
        // whether it is unique or
        // repeated, it hasn't
        // stored previously
        temp[j++] = arr[n-1];
         
        // Modify original array
        for (int i = 0; i < j; i++)
            arr[i] = temp[i];
     
        return j;
    }
     
    public static void Main ()
    {
        int []arr = {1, 2, 2, 3, 4, 4, 4, 5, 5};
        int n = arr.Length;
         
        n = removeDuplicates(arr, n);
 
        // Print updated array
        for (int i = 0; i < n; i++)
            Console.Write(arr[i] + " ");
    }
}
 
// This code is contributed by nitin mittal.


Javascript


C++
// C++ program to remove duplicates in-place
#include
using namespace std;
 
// Function to remove duplicate elements
// This function returns new size of modified
// array.
int removeDuplicates(int arr[], int n)
{
    if (n==0 || n==1)
        return n;
 
    // To store index of next unique element
    int j = 0;
 
    // Doing same as done in Method 1
    // Just maintaining another updated index i.e. j
    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;
}
 
// Driver code
int main()
{
    int arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // removeDuplicates() returns new size of
    // array.
    n = removeDuplicates(arr, n);
 
    // Print updated array
    for (int i=0; i


Java
// simple java program to remove
// duplicates
 
class Main
{
    // Function to remove duplicate elements
    // This function returns new size of modified
    // array.
    static int removeDuplicates(int arr[], int n)
    {
        if (n == 0 || n == 1)
            return n;
      
        // To store index of next unique element
        int j = 0;
      
        // Doing same as done in Method 1
        // Just maintaining another updated index i.e. j
        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[] = {1, 2, 2, 3, 4, 4, 4, 5, 5};
        int n = arr.length;
         
        n = removeDuplicates(arr, n);
  
        // Print updated array
        for (int i=0; i


C#
// simple C# program to remove
// duplicates
using System;
 
class GfG {
     
    // Function to remove duplicate
    // elements This function returns
    // new size of modified array.
    static int removeDuplicates(int []arr, int n)
    {
         
        if (n == 0 || n == 1)
            return n;
     
        // To store index of next
        // unique element
        int j = 0;
     
        // Doing same as done in Method 1
        // Just maintaining another updated
        // index i.e. j
        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 ()
    {
        int []arr = {1, 2, 2, 3, 4, 4,
                                 4, 5, 5};
        int n = arr.Length;
         
        n = removeDuplicates(arr, n);
 
        // Print updated array
        for (int i = 0; i < n; i++)
            Console.Write(arr[i] + " ");
    }
}
 
// This code is contributed by parashar.


Javascript


输出:

1 2 3 4 5

时间复杂度:O(n)
辅助空间:O(n)
方法2:(恒定的额外空间)
只需为方法 1 中为不同数组维护的同一数组维护一个单独的索引。

C++

// C++ program to remove duplicates in-place
#include
using namespace std;
 
// Function to remove duplicate elements
// This function returns new size of modified
// array.
int removeDuplicates(int arr[], int n)
{
    if (n==0 || n==1)
        return n;
 
    // To store index of next unique element
    int j = 0;
 
    // Doing same as done in Method 1
    // Just maintaining another updated index i.e. j
    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;
}
 
// Driver code
int main()
{
    int arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // removeDuplicates() returns new size of
    // array.
    n = removeDuplicates(arr, n);
 
    // Print updated array
    for (int i=0; i

Java

// simple java program to remove
// duplicates
 
class Main
{
    // Function to remove duplicate elements
    // This function returns new size of modified
    // array.
    static int removeDuplicates(int arr[], int n)
    {
        if (n == 0 || n == 1)
            return n;
      
        // To store index of next unique element
        int j = 0;
      
        // Doing same as done in Method 1
        // Just maintaining another updated index i.e. j
        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[] = {1, 2, 2, 3, 4, 4, 4, 5, 5};
        int n = arr.length;
         
        n = removeDuplicates(arr, n);
  
        // Print updated array
        for (int i=0; i

Python3

# Python3 program to remove
# duplicate elements

# This function returns new 
# size of modified array
def removeDuplicates(arr, n):
    if n == 0 or n == 1:
        return n

    # To store index of next
    # unique element
    j = 0

    # Doing same as done
    # in Method 1 Just
    # maintaining another 
    # updated index i.e. j
    for i in range(0, n-1):
        if arr[i] != arr[i+1]:
            arr[j] = arr[i]
            j += 1

    arr[j] = arr[n-1]
    j += 1
    return j

# Driver code
arr = [1, 2, 2, 3, 4, 4, 4, 5, 5]
n = len(arr)

# removeDuplicates() returns
# new size of array.
n = removeDuplicates(arr, n)

# Print updated array
for i in range(0, n):
    print (" %d "%(arr[i]), end = " ")


C#

// simple C# program to remove
// duplicates
using System;
 
class GfG {
     
    // Function to remove duplicate
    // elements This function returns
    // new size of modified array.
    static int removeDuplicates(int []arr, int n)
    {
         
        if (n == 0 || n == 1)
            return n;
     
        // To store index of next
        // unique element
        int j = 0;
     
        // Doing same as done in Method 1
        // Just maintaining another updated
        // index i.e. j
        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 ()
    {
        int []arr = {1, 2, 2, 3, 4, 4,
                                 4, 5, 5};
        int n = arr.Length;
         
        n = removeDuplicates(arr, n);
 
        // Print updated array
        for (int i = 0; i < n; i++)
            Console.Write(arr[i] + " ");
    }
}
 
// This code is contributed by parashar.

Javascript


输出:

1 2 3 4 5