# Sets and creating a new set containing a common number

• February 25th, 2010, 08:02 AM
mds1256
Sets and creating a new set containing a common number
I have 4 Sets that contains numbers.

I need to find the common numbers from all the sets and store this in another set

e.g.

Set1 = 1,5,6,9,10,12,15
Set2 = 3,5,6,9,60,80
Set3 = 1,5,6,8,9
Set4 = 1,5,6,90,500,1000

So in essence i need to find the numbers that are contained it every set, in this case its 5 and 6.

I then need to store these in another set.

I have no problems create a new set etc but struggling with how to loop through all the sets and pick the common number

Any help would be appriciated, its more of a concept request than a code request, e.g. do i use the contains() method

Thanks
• February 25th, 2010, 10:31 AM
helloworld922
Re: Sets and creating a new set containing a common number
A simple O(nlogn) (actually, depends on which sorting algorithm you use, but this is using quicksort) method is to put everything you have there into one sorted array. Then work your way through the list, and if find any duplicate values (they will be right next to each other), put that into your set.
• February 26th, 2010, 05:00 PM
mds1256
Re: Sets and creating a new set containing a common number
thanks :-)