C + + deletes duplicate data within the array

Source: Internet
Author: User
Tags int size min sort

There was no good idea at the time when I had a problem with the written test. I think of it today, so I write down my own way of solving it.

1. main.cpp

1./**
2. * Question Description:
3. * Delete duplicate data in the array
4. *
5. * A solution:
6. * You can sort the array before you delete it
7. * As a sorted array of integers: 1, 1, 2, 2, 3
8. */
9.
#include <iostream>
11.
12.using std::cout;
13.using Std::endl;
14.using Std::swap;
15.
16./**
17. * Print Array
18. */
19.template<class t>
20.void PrintArray (const T array[], const int size);
21st.
22./**
23. * Sort the array, select the sort
24. */
25.template<class t>
26.void sort (T array[], const int size);
27.
28./**
29. * For the data that has been sorted,
30. * Delete duplicate data in the array
* @return int Delete the size of the array after duplicate data
32. */
33.template<class t>
34.int Deleterepeateddata (T array[], const int size);
35.
36.int Main (int argc, char *argv[]) {
array[int] = {9, 1, 1, 8, 2, 3, 3, 4, 3, 3, 5, 9, 7, 8, 2, 6, 9, 1, 9, 0, 9, 0};
int size = sizeof (array)/sizeof (int);
cout<< "A initial int array:" <<endl;
PrintArray (array, size);
41.
cout<< "\nafter sort:" <<endl;
. Sort (array, size);
PrintArray (array, size);
45.
cout<< "\nafter Delete repeated data:" <<endl;
Size = Deleterepeateddata (array, size);
PrintArray (array, size);
49.}
50.
51./**
52. * Print Array
53. */
54.template<class t>
55.void PrintArray (const T array[], const int size) {
for (int i=0; i<size-1; i++) {
cout<<array[i]<< ",";
58.}
cout<<array[size-1]<<endl;.
60.}
61.
62./**
63. * Sort the array, select the sort
64. */
65.template<class t>
66.void sort (T array[], const int size) {
for (int i=0; i<size-1; i++) {
int min = i;
for (int j=i+1; j<size; J + +) {
if (Array[min] > Array[j]) {
min = j;
72.}
73.}
if (min!= i) {
(Array[i], array[min]);
76.}
77.}
78.}
79.
80./**
81. * For the data that has been sorted,
82. * Delete duplicate data in the array
* @return int Delete the size of the array after repeating data
84. */
85.template<class t>
86.int Deleterepeateddata (T array[], const int size) {
A. Int j = 0;
for (int i=0; i<size-1; i++) {
(Array[i] = = Array[i+1]) {
i++;
91.}
Array[j++] = Array[i];
93.}
A. return j;
95.}

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.