I don’t understand how to do this removal of duplicate elements, help. Apparently I wrote some nonsense after entering the array 🙂 Help, how to delete correctly
#include & lt; iostream & gt;
using namespace std;
int main () {
int size, m, i;
cin & gt; & gt; size;
int * arr = new int [size];
for (m = 0; m & lt; size; m ++) {
cin & gt; & gt; arr [m];
}
for (m = 0; m & lt; size; m ++) {//
for (i = 0; i & lt; size; i ++) {
if (arr [i] == arr [i + 1]) {
delete [] arr;
size--;
}
}
}
}
Answer 1, authority 100%
In the given solution, all the same are shifted to the tail of the array with decreasing length.
The tail can be trimmed or ignored.
for (m = 0; m & lt; size; m ++) {//
cout & lt; & lt; arr [m] & lt; & lt; endl;
for (i = m + 1; i & lt; size; i ++) {
if (arr [m] == arr [i]) {
for (int k = i; k & lt; size - 1; k ++) {
arr [k] = arr [k + 1];
}
size--;
}
}
}
Answer 2, authority 50%
Possible solution is through writing unique values to a new array.
The code is given when ALL duplicate values are removed.
If necessary, you can rework the code to remove all duplicates except the first one (check all values of the original array with the final array, not with the primary one).
void main ()
{
int k = 0;
int i = 0;
int firstSize = 0;
int outSize = 0;
int controlNumb = 0;
setlocale (LC_ALL, "Russian");
std :: cin & gt; & gt; firstSize;
int * arrayFirst = new int [firstSize];
int * arrayOut = new int [firstSize];
for (int count = 0; count & lt; firstSize; count ++)
std :: cin & gt; & gt; arrayFirst [count];
for (int count = 0; count & lt; firstSize; count ++)
arrayOut [count] = 0;
for (i = 0; i & lt; firstSize; i ++) // For each number in the array
{
controlNumb = 1; // Update the control value
for (k = 0; k & lt; firstSize; k ++) // check each value of the array
if ((k! = i) & amp; (arrayFirst [i] == arrayFirst [k])) controlNumb = 0; // except for itself, and if any of them match, assign the control value 0
if (controlNumb) // if the control value remains 1, then add 1 to the conditional size of the Final array and write a unique number to the end
{
outSize ++;
arrayOut [outSize - 1] = arrayFirst [i];
}
}
if (! outSize) // if the conditional size of the final array remains equal to zero
std :: cout & lt; & lt; "There are no unique values in the array";
else
for (int count = 0; count & lt; outSize; count ++) // display the values of the final array separated by a space
std :: cout & lt; & lt; arrayOut [count] & lt; & lt; "";
}
Answer 3
There are many options for solving your problem.
IMHO work with a vector. Copy your data to vector
int * arr = new int [10];
std :: vector & lt; int & gt; v {arr, arr + 10};
...
if (arr) {
delete [] arr;
}
For you, I gave 2 options for solving the problem:
1) Pre-sorted and using std :: unique
2) Preserving the sequence
# include & lt; iostream & gt;
#include & lt; algorithm & gt;
#include & lt; vector & gt;
#include & lt; iterator & gt;
int main () {
// Option for a pre-sorted sequence
std :: vector & lt; int & gt; v {7,6,1,2,3,1,2,3,3,4,5,4,5,6,7};
std :: copy (std :: begin (v), std :: end (v), std :: ostream_iterator & lt; int & gt; {std :: cout, ""});
std :: cout & lt; & lt; std :: endl;
std :: sort (v.begin (), v.end ());
Auto Last = std :: unique (v.begin (), v.end ());
V.ERASE (Last, V.End ());
STD :: Copy (STD :: Begin (V), std :: end (v), std :: ostream_terator & lt; int & gt; {std :: cout, ""});
STD :: COUT & LT; & LT; STD :: ENDL;
STD :: COUT & LT; & LT; STD :: ENDL;
// Option for non-ruled sequence
std :: vector & lt; int & gt; v2 {7,6,1,2,3,1,2,3,3,4,5,4,5,6,7};
STD :: COPY (STD :: Begin (v2), std :: end (v2), std :: ostream_terator & lt; int & gt; {std :: cout, ""});
STD :: COUT & LT; & LT; STD :: ENDL;
For (AUTO IT {STD :: Begin (v2)}; it! = std :: end (v2); it = std :: next (IT)) {
For (Auto sub_it {std :: next (it)}; sub_it! = std :: end (v2); sub_it = std :: next (sub_it)) {
if (* sub_it == * IT) {
V2.ERASE (sub_it);
--sub_it;
}
}
}
STD :: COPY (STD :: Begin (v2), std :: end (v2), std :: ostream_terator & lt; int & gt; {std :: cout, ""});
STD :: COUT & LT; & LT; STD :: ENDL;
Return 0;
}