DEV Community

Discussion on: Daily Challenge #208 - Delete Occurrences of an Element

Collapse
 
cipharius profile image
Valts Liepiņš

Haskell:

import Data.List (foldl')
import qualified Data.IntMap.Strict as Map

deleteNth :: Int -> [Int] -> [Int]
deleteNth n = reverse . fst . foldl' fn ([], Map.empty)
  where
    fn (out, set) i =
      case Map.lookup i set of
        Nothing       -> (i:out, Map.insert i 1 set)
        Just x
          | x < n     -> (i:out, Map.insert i (x+1) set)
          | otherwise -> (out, set)