Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

HashSets

Problem Description

The Set interface of the Java Collections framework provides the features of the mathematical set in Java. It extends the Collection interface.

Unlike the List interface, sets cannot contain duplicate elements.

Since Set is an interface, we cannot create objects from it.

In order to use functionalities of the Set interface, we can use these classes:

  • HashSet
  • LinkedHashSet
  • EnumSet
  • TreeSet

In Java, we must import java.util.Set package in order to use Set.

// Set implementation using HashSet
Set<string> animals = new HashSet<>()

Here, we have created a Set called animals. We have used the HashSet class to implement the Set interface.

Methods of Set:

The Set interface includes all the methods of the Collection interface. It’s because Collection is a super interface of Set.

Some of the commonly used methods of the Collection interface that’s also available in the Set interface are:

  • add() - adds the specified element to the set
  • addAll() - adds all the elements of the specified collection to the set
  • remove() - removes the specified element from the set
  • clear() - removes all the elements from the set
  • size() - returns the length (number of elements) of the set
  • toArray() - returns an array containing all the elements of the set
  • contains() - returns true if the set contains the specified element

To learn about more methods of the Setinterface, visit Java Set (official Java documentation).

LinkedHashSet:

The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements in the order in which they were inserted. When cycling through LinkedHashSet using an iterator, the elements will be returned in the order in which they were inserted.

Task:

Given an integer array A you need to find the total number of unique elements in that array A.


Problem Constraints

1 <= |A| <= 105

1 <= A[i] <= 109

 

Input Format

The first-line contain a single integer N denoting the size of the array A.

Next N lines each contain a single integer denoting the array A elements.


Output Format

Print a single integer denoting the total number of unique elements in that array A.


Example Input

Input 1;

 5
 1
 1
 3
 1
 2



Example Output

Output 1;

 3

 Example Explanation

Explanation 1;

 Unique elements are : [1, 2, 3]

 



Start solving HashSets  on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test