Delete duplicate data in C + + and output (equivalent to sort-u inside shell script)

Source: Internet
Author: User

Question://give you an array, a[]={1,1,1,1,1,2,2,2,2,3,3,3,4,5,6}//to output the result is 1,2,3,4,5,6. (Removal of duplicate numbers, requiring time space considerations). #include <iostream>using namespace std;struct node{int data; Node *next; Node ():d ata ( -1), Next (NULL) {}};//time complexity decreases significantly, but adds a certain degree of space complexity. Class Hash{public:hash (int a[],int n) {nodetable = new node[7];for (int i=0;i<n;i++) {int index = Hashindex (A[i]);//Find a[ I], and in the hash table to find whether the value exists, does not exist we are inserted in the form of a list. The nodetable[index].data=1;//is initialized to 1, indicating that there is no value below the subscript, and if the index equals the subscript, mark the subscript as 1, indicating that something is in store! node* p = nodetable[index].next;while (P!=null && p->data!=a[i]) {p=p->next;} if (p==null) {node *s = new Node (); s->data = A[i]; Nodetable[index].next=s;}}} void Show () {for (int i=0;i<7;i++) {if (nodetable[i].data!=-1) {Node *p = nodetable[i].next;while (p!=null) {cout< < "" <<p->data<< ";p = P->next;}}} Cout<<endl;} int hashindex (int x) {return x%7;} Private:node *nodetable;}; int main () {int a[]={1,2,2,1,1,1,3,4,5}; Hash sh (a,9); Sh. Show (); return 0;} #include <iostream> #include <stdlib.h>using Namespace std;//rough Cut method, time complexity is slightly higher, space complexity is low. void grial (int a[],int& n)//n is passed in by reference, because the size of the array will change dynamically when I delete a repeating value, and I don't need to record the actual value of N. {int I=0;int j;int k;for (;i<n;) {for (j=i+1;j<=n;j++) {if (A[i]==a[j]) {for (k=j;k<n;k++) {a[k]=a[k+1];//Delete with A[i ] duplicate value, and n minus 1;}n--;} if (j>=n) {i++;}} if (A[i]==a[i+1]) {for (k=i;k<n;k++) {a[k]=a[k+1];} n--;}}} int main () {int a[100];for (int i=0;i<100;i++) {a[i]=rand ()%100;} int b=99; Grial (A, b); for (int i=0;i<b;i++) {Cout<<a[i]<<endl;} return 0;}

Delete duplicate data in C + + and output (equivalent to sort-u inside shell script)

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.