

-----------------------------------

ScarletSkyFury

Thu Nov 29, 2007 12:44 pm


Just a little help

-----------------------------------

Allo

Well the problem is actually extremely simple, which is makes me frustrated cause i can't seem
to get my head around it.

Well here it is:

I get the user to fill up 2 arrays with integer numbers

Then i have to make a union of those arrays and put them into a 3rd array

ex:

1st Array: 2,3,2
2nd Array: 4,5,2

But repeated numbers arent added into the third array, hence in this case the 3rd Array would be:

3rd Array: 2,3,4,5

Thanks in advanced xD


-----------------------------------

HeavenAgain

Thu Nov 29, 2007 12:56 pm


RE:Just a little help

-----------------------------------

Hmmm.... go through the first array first, record down ith number (in the 3rd array), and number recorded shows up again, dont count it (so you probably need a nested loop)
and do the same for the 2nd array, using 3rd array to check

and then just simply sort the 3rd array :roll: no?


-----------------------------------

wtd

Thu Nov 29, 2007 7:57 pm


RE:Just a little help

-----------------------------------

Let's think about some base cases.

Union of two empty arrays?

Union of one non-empty array and an empty array?


-----------------------------------

HeavenAgain

Thu Nov 29, 2007 9:46 pm


Re: RE:Just a little help

-----------------------------------

Let's think about some base cases.

Union of two empty arrays?

Union of one non-empty array and an empty array?
that doesnt matter much does it? if its empty, then length of array is 0, which means the "searching" loop wont even run? :?


-----------------------------------

wtd

Thu Nov 29, 2007 11:10 pm


RE:Just a little help

-----------------------------------

I am trying to get the OP to think algorithmically; to think before writing code.

But to put that into code:

module SetUtils (union) where

union [] [] = []
union a [] = a
union a (y:ys) = 
  union (b ++ if hasMember b y then [] else [y]) ys
  where b = union [] a

hasMember [] _ = False
hasMember (x:xs) v = x == v || hasMember xs v

