I have a problem in which we are given 2 sets A and B and we have to find set C which contains elements common to A and B.The set A and B can contain duplicates and set C is expected to contain distinct elements(i.e no repetition of the same element) common to A and B.

As a solution to this, I thought that first all the duplicates in A and B can be removed and then set C can be formed by comparing each element of A and B.
Is there any other efficient way to solve the problem?

Just post your idea and I will do the coding. Thanks in advance!

Why don't you think of the efficient way.

Here are your sets A and B. Do these by hand and see if you can think of anything.

A = [0,83,71,39,9,34,77,20,86,9,82,45,88,44,34,38,24,61,48,3,96,66,
47,1,95,54,83,49,9,5,78,78,47,98,54,6,3,1,74,87,1,75,100,18,21,94,
19,15,4,78,72,48,63,56,18,31,83,84,5,65,54,88,38,93,83,16,11,21,80,84]

B = [17,85,78,41,85,56,9,12,83,100,59,66,56,86,66,82,98,92,14,43,96,69,
57,21,97,76,10,72,5,20,20,10,29,37,89,92,71,42,41,58,19,23,30,79,91,67,
57,48,77,11,98,92,60,87,93,48,28,37,3,48,43,58,62,10,10,18,35,28,20,2]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.