020-002-008

Removing Duplicates with HashSet

Easy

Problem Description

Removing Duplicates with HashSet

In this problem, you will implement a method that takes an array of strings, removes duplicates using HashSet, and returns the sorted unique elements as a comma-separated string.

Learning Objective: Understand how to remove duplicate elements from a list using HashSet characteristics

Overview

Set is a collection that does not allow duplicate elements. By adding list elements to a HashSet, duplicates are automatically removed.

Specifications

  • Implement a removeDuplicates method
  • Parameter: an array of strings (may contain duplicates)
  • Use HashSet to remove duplicates
  • Sort the result and return as a comma-separated string

Method Signature

public static String removeDuplicates(String[] items)

Examples

removeDuplicates(["Apple", "Banana", "Apple", "Cherry", "Banana"])
→ "Apple,Banana,Cherry"

removeDuplicates(["Dog", "Cat", "Dog"])
→ "Cat,Dog"

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up