C++ 从已排序的数组中删除重复项
我们将获得一个已排序的数组作为输入。我们的任务是从集合中删除重复的元素并将函数返回到主函数,从那里我们可以开始打印新的已排序的数组,而没有重复项。
我们可以使用额外的空间和使用恒定的额外空间两种方式来解决这个问题,下面将讨论具体的代码和输出。
C++代码:使用额外空间
// Here in the below notes, we are writing down the C++ programming language
// code to demonstrate the concept of a Simple C++ program to
// Remove duplicates from the sorted array with code and example, and their
// respective output.
#include
using namespace std;
// the below function which we have written will help us with removing the
// duplicate elements in the array which are passed into it as an argument
// it will return us the size of the new modified array once finishing
// the operation of removing the duplicate elements
int removeDuplicates(int arr[], int n)
{
// this is a small (if) check condition where we will be checking if
// whether the element which is passed as an argument contains one element
// or no elements and return the n value if the condition is true.
if (n == 0 || n == 1)
return n;
int temp[n];
// from j equals to 0 onwards; we start traversing the elements in the
// array passed as an argument
// we have an uninitialised variable j to 0, which can then start
// traversing from 0 to the ray's size w, n-1.
int j = 0;
// this simple for loop has a simple check condition where we check if
// the present element where we are trying to store is equal to the next
// element or not
for (int i = 0; i < n - 1; i++)
if (arr[i] != arr[i + 1])
temp[j++] = arr[i];
// Store the last element as whether it is unique or
// repeated, it hasn't been used previously
// whether the last part of the array is individual, a repeated one
// we are trying to keep here what was not, therefore in the code
temp[j++] = arr[n - 1];
// we are trying to store the array in the temporary array created
// after modifying the original array using a simple for loop
for (int i = 0; i < j; i++)
arr[i] = temp[i];
return j;
}
// the main driver code functionality starts from here
int main()
{
int arr[] = { 21, 22, 22, 23, 24, 24, 24, 25, 25 };
int n = sizeof(arr) / sizeof(arr[0]);
// the C++ programming language has in-built removeDuplicates()
// function, which helps us with not writing huge lines of code instead
// do the task in lightning less time complexity.
// after operating, it returns to us the size of the new array
n = removeDuplicates(arr, n);
// the below small for-loop code snippet helps us with printing down the
// updated array after removing the duplicate elements to the display
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
输出:
31 32 33 34 35
常数额外空间
C++ code
// Here in the below notes, we are writing down the C++ programming language
// code to demonstrate the concept of a Simple C++ program to
// Remove duplicates from the sorted array with code and example, and their
// respective output.
#include
using namespace std;
// the remove duplicates in-built function helps us not to write
// the same extra lines of code
int removeDuplicates(int arr[], int n)
{
if (n==0 || n==1)
return n;
// from j equals to 0 onwards; we start traversing the elements in the
// array passed as an argument
int j = 0;
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;
}
// the main driver code functionality starts from here
int main()
{
int arr[] = {31, 32, 32, 33, 34, 34, 34, 35, 35};
int n = sizeof(arr) / sizeof(arr[0]);
n = removeDuplicates(arr, n);
// the below small for-loop code snippet helps us with printing down the
// updated array after removing the duplicate elements from the display
for (int i=0; i
输出:
31 32 33 34 35