2

Im trying to figure out how to create a new list that has only the elements that occur only once. I can't use recursion either.

This is a small part of a bigger function. Im trying to write a function to get the Intersect of two sets. Basically i combined the sets. Then sorted. Then i want to combine that with set B and get rid of all the duplicates in that set. Unless someone else knows an easier way.

I can use high order functions, but i can't use recursion.

Thanks

Edit: Example:

[1,2,3,3,4,4,5,5] Should come out as [1,2]

Matt
  • 7,049
  • 7
  • 50
  • 77

4 Answers4

2

This question was already asked before and essentially you just want to group them together by counting the number of elements and then only extracting those with a count of one. (probably using 'filter')

Please see Counting unique elements in a list

Community
  • 1
  • 1
Robert Massaioli
  • 13,379
  • 7
  • 57
  • 73
  • The question is tagged as homework, so he probably wants hints more than someone to solve it for him – Daenyth Sep 28 '10 at 02:42
  • you're a genius if i can use group. – Matt Sep 28 '10 at 02:43
  • @Daenyth yeah but the first part of doing homework is doing your research. I wanted to show that perhaps a little more time searching would have led to the answer, maybe. – Robert Massaioli Sep 28 '10 at 03:42
  • @Matt hopefully you can and then hopefully it helps; better yet if you understand why it works then that is good enough for me. – Robert Massaioli Sep 28 '10 at 03:43
  • I had searched on here, but couldnt find anything as i didn't want to count unique elements, but just gather the ones that had no duplicates. But i didn't even know about the "group" function. I did come up with a way to do what i need to do with group, just need to see if i can use it now. – Matt Sep 28 '10 at 03:48
1

I hope this hint is sufficiently vague. :)

Think about grouping duplicate elements together.

dino
  • 1,123
  • 9
  • 13
  • I know how to zip and then get rid of the duplicates. But i only want the elements that occur only once. Edited my question with an example. – Matt Sep 28 '10 at 01:55
  • Do you think it would help if the, now adjacent, elements were themselves in smaller, internal lists? – dino Sep 28 '10 at 02:10
  • Oh I see, I get no love for trying to help the OP to come up with the answer on his own. bah – dino Sep 28 '10 at 18:28
  • Thanks for trying to help, but i would have still sat there scratching my head. I didn't know about the group function in haskell. – Matt Sep 28 '10 at 23:20
1

Think about what data structure you'd use to keep track of how many times each thing shows up.

Daenyth
  • 35,856
  • 13
  • 85
  • 124
1

How about:

concat $ filter (null . tail) $ group [1,2,3,3,4,4,5,5]
stusmith
  • 14,003
  • 7
  • 56
  • 89