74 use bitset to output repeated elements in the array

Source: Internet
Author: User
Tags bitset

[Link to this article]

Http://www.cnblogs.com/hellogiser/p/using-bitset-to-print-duplicate-elements-of-array.html

[Question]

An array has l elements in the range of 0 to n, where n is <32000, but the exact size of N is unknown. L there are several repeated elements in each element, with only 4 kb of memory. How can I output repeated elements?

[Analysis]

Since the value of the array is between a range [), we naturally want to use bitset for processing. If bitset is used, one integer can be expressed as one bit. 1 byte can represent 8 different integers, and 4 kb can represent 32 KB different integers. Because 32 KB = 32*1024> 32000, the 4 kb memory is sufficient to indicate 32000 different numbers. The core is the access to one integer in the bitset. Open an array that can store N/32 + 1 int numbers, then the number X is stored in the X % 32 bit of the array [x/32] integer.

Instant MessagingI = x/32, j = x % 32, array [I] | = (1 <j)

[Code]

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  // 74_bitset.cpp: defines the entry point for the console application.
//

# Include "stdafx. H"
# Include "iostream"
# Include <ctime>
# Include <algorithm>
Using namespace STD;

Class bitset
{
Public:
Bitset (INT range)
{
// [0, range)
M_nrange = range;
M_nlength = m_nrange/32 + 1;

Bitset = new int [m_nlength];
// Init all with 0
For (INT I = 0; I <m_nlength; I ++)
{
Bitset [I] = 0;
}
}

~ Bitset ()
{
Delete [] bitset;
}

Void set (INT number)
{
Int I = Number/32;
Int J = Number % 32;
Bitset [I] | = (1 <j );
}
Bool get (INT number)
{
Int I = Number/32;
Int J = Number % 32;
Return (bitset [I] & (1 <j ))! = 0;
}

Void output ()
{
For (INT I = 0; I <m_nrange; I ++)
{
If (get (I ))
{
Cout <I <"";
}
}
Cout <Endl;
}
PRIVATE:
Int * bitset;
Int m_nrange; // range of numbers
Int m_nlength; // Len of Array
};

Void print (int * a, int N)
{
For (INT I = 0; I <n; I ++)
{
Cout <A [I] <"";
}
Cout <Endl;
}

Void printduplicatenumbers (int * a, int N, int count)
{
Cout <"array numbers =============================\ N ";
Print (A, N );
Cout <"duplicate numbers =============================\ N ";
Bitset BS (count );
For (INT I = 0; I <n; I ++)
{
If (BS. Get (A [I])
Cout <A [I] <"";
Else
BS. Set (A [I]);
}
Cout <Endl;
Cout <"existing numbers ===========================\ N ";
BS. Output ();
}

Void test_defualt ()
{
Const int n = 20;
Const int range = 12;

Srand (unsigned INT) Time (null ));
Int A [n];
For (INT I = 0; I <n; I ++)
{
A [I] = rand () % range;
}
Printduplicatenumbers (A, N, range );
}

Int _ tmain (INT argc, _ tchar * argv [])
{
Test_defualt ();
Return 0;
}
/*
Array numbers ==================================
7 0 2 8 0 3 0 3 2 1 7 5 11 5 4 11 1 0 2 4
Duplicate numbers =============================
0 0 3 2 7 5 11 1 0 2 4
Existing numbers ================================
0 1 2 3 4 5 7 8 11
*/

[Link to this article]

Http://www.cnblogs.com/hellogiser/p/using-bitset-to-print-duplicate-elements-of-array.html

74 use bitset to output repeated elements in the array

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.