Home > Back-end >  How to eliminate duplicate elements in an array
How to eliminate duplicate elements in an array

Time:09-25

How to eliminate duplicate elements in an array?
Var arr=,2,3,3,4,4,5,5,6,1,9,3,25,4 [1]
Output: 1,2,3,4,5,6,9,25

CodePudding user response:

Stupid idea, from the beginning of the number 2, compared with all previous number, if there is the same, the number is changed to a particular value (such as 0, 1, the maximum or minimum)
N number, compared with the n - 1 in front of the number; Until the last number
The output is not special values in the array on the number of all

STL there should be a convenient way

CodePudding user response:

Can use tree function to sort first, and then use cycle to find different number, use the tree as follows,
Int comp (const void * p, const void * q)
{
Return (int *) (* p - * (int *) q);
}

int i;
An int array []={6, 8, 2, 8, 1, 0};
Tree array, 6, sizeof (int), comp);

Sorted array internal data as follows,
6 8 8 0 1 2

CodePudding user response:

 
Int arr []={1,2,3,3,4,4,5,5,6,1,9,3,25,4}
VectorSort (iVec. The begin (), iVec end ());
IVec. Erase (unique (iVec. The begin (), iVec. The end ()), iVec. The end ());

CodePudding user response:

Cows don copy code not to copy the complete
http://www.cnblogs.com/oomusou/archive/2007/05/27/761532.html

CodePudding user response:

 

#include
#include
#include

using namespace std;
Int main ()
{
Int arr []=,2,3,3,4,4,5,5,6,1,9,3,25,4 {1};
The vector & lt; Int & gt; IVec (arr, arr + 14);
Vector : : iterator iter.
Sort (iVec. The begin (), iVec end ());
IVec. Erase (unique (iVec. The begin (), iVec. The end ()), iVec. The end ());
For (iter=iVec. The begin (); Iter!=iVec. End (); + + iter)
Cout & lt; <"" & lt; <* iter.

Cout & lt; system("pause");
return 0;
}
  • Related