The Utils Folder 10 exercises

Inference in Generic Functions

In this exercise we have a function named uniqueArray which accepts an array as an argument. It converts this array into a set before finally returning a new array. This function is designed to eliminate duplicate values from the array, a typical requirement when manipulating array data.


Loading exercise


00:00 In this exercise, we have a unique array function, which takes in an array, which is currently typed as any array. And then we basically turn it into a set first, and then create an array from it. This is a common pattern for when you want to just have some unique values inside your array. So we have here unique array,

00:18 which has a bunch of ones and a couple of fours in. And then when the result we get out of it is 1, 2, 3, 4, 5, because those are the unique values. And same down here with strings. So we've got A, B, B, C, C, C, and we should just get A, B, C at the end of it. Now this works at the runtime level, but it's not working on the type level.

00:37 So if we look here at results, it's an any array. And then in the string test, it's any array here too. So your job is to work out how we can change unique array. So first of all, we're gonna need to add a type parameter to it. But you notice that in neither of these situations are we actually adding a type parameter.

00:55 So we don't need to add a type parameter apparently for this to work. So add a type parameter to unique array, figure out how to use it, and work out what other type annotations you need to get this test working. Good luck.