Avatar ·

How to create a generic array in Java? (Generic Array)

Hi everyone! I started learning generics in Java and ran into a problem that I can't solve for several hours. I need to create a method that takes an array of any type and returns a new array containing only unique elements. For example, I want to pass `String[]` and get `String[]` without duplicates, or pass `Integer[]` and get `Integer[]`. Here is my code: ```java public class UniqueArray { public static T[] getUnique(T[] input) { // First I want to create an array for the result, but I don't know its size T[] result = new T[input.length]; // Compilation error! // ... rest of the logic return result; } } ``` The compiler gives an error: "Cannot create a generic array of T". I read that in Java you cannot