020-002-011

Basic HashSet Operations

Easy

Problem Description

Basic HashSet Operations

In this problem: You will create a program using HashSet from the collections framework to add elements, check for duplicates, and get the size.

Learning Objective: Understand basic HashSet operations (add, contains, size) and duplicate elimination behavior

Overview

HashSet is a collection that does not allow duplicates. Adding the same element multiple times keeps only one copy. add() returns true if the element was added, false if it already exists.

Specifications

  • Create a HashSet
  • Add "Apple", "Banana", "Cherry", "Apple" (duplicate)
  • Print the return value of each add()
  • Print final size and check for "Apple" existence

Output Format

Added Apple: true
Added Banana: true
Added Cherry: true
Added Apple again: false
Size: 3
Contains Apple: true
Contains Grape: false

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