Haskell Program to Convert Array to Set (HashSet)


In Haskell, we will convert Array to Set (HashSet) by using fromList, nub and foldr functions. In the first example, we are going to use ( let set = Set.fromList arr) and in the second example, we are going to use ( let set = nub arr). And in the third example, we are going to use (let set = foldr Set.insert Set.empty arr).

Algorithm

  • Step 1 − The Data.Set module is imported to work over set.

  • Step 2 − The program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.

  • Step 3 − The array is initialized with some values.

  • Step 4 − The set named, “set” is being initialized to convert array to the set by using fromList function as,

    let set = Set.fromList arr.

  • Step 5 − The resultant set is printed to the console by using print function.

Example 1

In this example, we first import the Data.Set module, which provides a set implementation based on balanced binary trees. We then define an array named arr containing some elements, including duplicates. We then use the fromList function of the Set module to create a set named set from the elements in the array. Finally, we print the set using the print function.

import qualified Data.Set as Set

main :: IO ()
main = do
   let arr = [1, 2, 3, 2, 1, 4]
   let set = Set.fromList arr
   print set

Output

fromList [1,2,3,4]

Example 2

In this example, we first import the Data.List module, which provides various list-manipulation functions. We then define an array named arr containing some elements, including duplicates. We then use the nub function from the Data.List module to remove duplicates from the array and create a new list named set. Finally, we print the list using the print function.

import Data.List (nub)

main :: IO ()
main = do
   let arr = [1, 2, 3, 2, 1, 4]
   let set = nub arr
   print set

Output

[1,2,3,4]

Example 3

In this example, we first import the Data.Set module, which provides a set implementation based on balanced binary trees. We then define an array named arr containing some elements, including duplicates. We then use the foldr function to insert each element of the array into a new set starting from an empty set. The insert function inserts an element into a set, and the empty function creates an empty set. Finally, we print the set using the print function.

import qualified Data.Set as Set

main :: IO ()
main = do
   let arr = [1, 2, 3, 2, 1, 4]
   let set = foldr Set.insert Set.empty arr
   print set

Output

fromList [1,2,3,4]

Conclusion

In Haskell, an array is a collection of elements of the same type, indexed by integers. A set is a collection of distinct elements that can be used to efficiently check for membership and remove duplicates. A hashset is a specific type of set that uses a hash function to map elements to an array of buckets, allowing constant-time access to elements in many cases. Converting an array to a set (hashset) involves removing duplicates from the array and adding each unique element to a new set. There are various ways to perform this conversion in Haskell, depending on the desired implementation and requirements.

Updated on: 25-Apr-2023

117 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements