DEV Community

Discussion on: Daily Challenge #301 - Array Combos

Collapse
 
cipharius profile image
Valts Liepiņš

My solutions in Haskell.

The first solution - inefficient(O(n) = n^3), yet very elegant looking:

import Data.List (nub)

solve :: Eq a => [[a]] -> Int
solve = product . map (length . nub)
Enter fullscreen mode Exit fullscreen mode

Slightly better solution (O(n) = n^2 * log n) using containers library:

import qualified Data.Set as Set

solve :: Ord a => [[a]] -> Int
solve = product . map (Set.size . Set.fromList)
Enter fullscreen mode Exit fullscreen mode

And best solution (O(n) = n^2), but accepts only integers as input:

import qualified Data.IntSet as Set

solve :: [[Int]] -> Int
solve = product . map (Set.size . Set.fromList)
Enter fullscreen mode Exit fullscreen mode