Screening array arr [] array duplicate elements, consider the time complexity

Ideas:

Use of two arrays, A [] arr repeated to store all elements of another array B [] is an index value of arr element, the array element number of times corresponding to subscript value occurs. Time complexity is O (n)

Note that , B [] required to apply static arrays, because an overflow value if dynamic differences between the two adjacent array elements, then occurs; dynamic array a [] if required, because the number of elements simultaneously all elements are effective uncertain output, direct output to traverse.

 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 #define MAXSIZE 100
 5 
 6 int b[MAXSIZE] = { 0 };
 7 vector<int>a;
 8 int arr[5] = { 1,4,3,1,4 };
 9 
10 void repeat(int*arr, int n) {
11     for (int i = 0; i < n; i++) {
12         b[arr[i]]++;
13     }
14     for (int i = 0; i < MAXSIZE; i++) {
15         if (b[i] > 1)
16             a.push_back(i);
17     }
18 }
19 
20 int main() {
21     repeat(arr, 5);
22     for (int i = 0; i < a.size(); i++)
23         cout << a[i] << " ";
24     return 0;
25 }

 

Guess you like

Origin www.cnblogs.com/PennyXia/p/12654888.html