C ++ Algorithm for deleting repeated elements in an array (map), array map

Source: Internet
Author: User

C ++ Algorithm for deleting repeated elements in an array (map), array map
C ++ Algorithm for deleting repeated elements in an array (map)

<1. array deduplication>

I. Problem Description

In actual programming, we often encounter similar problems such as "Removing repeated numbers", that is,"Remove the duplicate"Problem. For example, "duplicate elements must be removed when a statistical sample is created ". The following describes how to solve these problems.

Ii. Solution

There are basically three ways to solve the above problems:

First, use the map container to filter out repeated elements (applicable to integer data and strings );

Type 2: remove duplicates using ArraysFirst, performSortAnd then traverse the array from the beginning to determine whether the data at the current position and the data at the next position are repeated. If the data is not repeated, add the data in the next position to the filtered array, set the benchmark data to the added data and continue the comparison. If it is repeated, skip this data to compare the following data (you can also compare strings). You need to use two pointers (one benchmark pointer and one cursor pointer );

The third method is to improve the second method., The second method needs to open up new space storage filtered elements, this method does not need to open up space, just with no repeated elements in the array to overwrite the repeated elements, the process is as follows:

0 1 2 3 4 5 6
1 1 2 3 3 3 4

(1) The benchmark pointer first points to the 0th positions of the array, and the cursor Pointer Points to the 1st positions to compare whether the data pointed to by the two pointers is equal. If the two pointers are equal, the cursor pointer moves one digit to the back, the reference pointer does not move and is compared again. If it is not equal, the data pointed to by the cursor is copied to the next position of the reference pointer, and the reference pointer and the cursor pointer are respectively moved from the current position to the back, compare again;

(2) termination condition: the cursor pointer exceeds the last digit of the array;

(3) An array before the current benchmark pointer is an array after deduplication (including the elements pointed to by the benchmark pointer );

Iii. Code Implementation

1. map container de-duplication implementation code

# Include
 
  
# Include
  Void inserttomap (int * a, int n, std: map
   
    
& Outmap); void sprintmap (std: map
    
     
& Coutmap); void main () {int a [10] = {1, 1, 2, 3, 4, 4, 5, 6, 6, 8}; std :: map
     
      
Resultmap; inserttomap (a, 10, resultmap); sprintmap (resultmap); system ("pause");} void inserttomap (int * a, int n, std: map
      
        & Outmap) {std: map
       
         Testmap; std: pair <std: map <int, int >:: iterator, bool> ret; for (int I = 0; I <n; I ++) {ret = testmap. insert ({a [I], 1}); if (! Ret. second) {++ testmap [a [I] ;}} outmap = testmap;} void sprintmap (std: map
        
          & Coutmap) {auto map_it = coutmap. cbegin (); while (map_it! = Coutmap. cend () {std: cout <"element" <map_it-> first <"appears as" <map_it-> second <
        
       
      
     
    
   
 

2. Open up array space de-duplication implementation code

Similar to the third method, this is not described here;

3. Remove code from the array space

# Include
 
  
# Include void removemultip (int * a, int n, int & cout); void main () {int a [10] = {1, 1, 2, 3, 4, 4, 5, 6, 6, 8}; std: sort (& a [0], & a [9] + 1); // array sorting int scount; // removemultip (a, 10, scount); for (int I = 0; I <= scount; I ++) {std :: cout <a [I] <std: endl;} system ("pause");} void removemultip (int * a, int n, int & cout) {if (n <= 1) return; int start = 0; // reference position int end = 1; // cursor position while (end
 

Iv. Summary

1. Concept of map container de-duplication

The unique key value cannot be inserted in the map container for constant or string array de-duplication, and the number of repeated elements can be counted with the ID of the return value of the map. insert function.(It is very useful to count the elements that appear most frequently in a set of data );

2. The idea of removing duplicates from array space

Before deduplication, ensure data orderliness, remove duplicate elements from the original array by using two pointers, and finally truncate the array to obtain the de-duplicated array;

<2. bug encountered when using the map container of C ++ (map/set iterator not dereferencable)>

map
 
  >::iterator it = shotList.find  (shotId);  while (it != shotList.end() &&      it->first.find(string("shot") + id) != string::npos)  {      if (it->second.first <= start && it->second.second >= start||      it->second.first <= end && it->second.second >= end)      {          shotResultList.push_back(it->first);      }      ++it;  }  
 

Note:

1. termination condition of the iterator in whileIt! = ShotList. end ();

2. it must be ++ it. it ++ reports an error.

<3. instance>

I. instance 1 -- map + dual for loop format

#include 
 
  #include
  using namespace std;int main(){    int a[10]={1,3,5,3,2,7,2,6,4,1};    int j=0;    map
   
     n;    pair
    
     ::iterator,bool> ret;    for(int i =0;i<10;i++)    {       ret = n.insert(pair
     
      (a[i],i));       if(ret.second!=false)           a[j++] = a[i];    }    for(int i=0;i
     
    
   
 

Ii. instance 2 -- set format

#include 
 
  #include 
  
   using std::cout;using std::endl;using std::set;int main(int argc, char* argv[]){    int ia[]={1,3,2,4,3,2,4,3,2,2};    set
   
     is(ia,ia+10);    for (set
    
     ::const_iterator c_i=is.begin();c_i!=is.end();++c_i)        cout<<*c_i<
                
     
    
   
  
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.