haskel check if number appears only once in the list

I want check if number appears only once in the list. I  wrote following code, but i don't know how to check if number already was there.
once :: [Int] -> Int -> Bool
once [] n = False
once (x:xs) n
  | x == n  = True
  | otherwise  = once xs n

Open in new window

Who is Participating?
fridomConnect With a Mentor CEO/ProgrammerCommented:
If the list is unorderd you have no chance, to implement it that  way. You have to run over the list over and over again and check it again and again. Howerver if you'd write it like this

It does work differently. there is a predicate applied to  the list checking if the element matches the searchedItem,  this elements are collected in another list and then I just  see if this  list has the length 1,which means the element in the list exactly once.

onceInList :: (Eq a) => [a] -> a -> Bool
onceInList aList item = length (filter (\el -> el = item) aList) == 1

Open in new window

sorry for the  follow up the el = item must be == sorry. a copy+paste error.
static86Author Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.