Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8163481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:04:14+00:00 2026-06-06T19:04:14+00:00

My Question Is: What’s a fast way to determine whether a number is contained

  • 0

My Question Is: What’s a fast way to determine whether a number is contained in a Collection to know whether to add it to the collection and maintain uniqueness. I would rather not iterate through the list if I can help it.

I have a List<Integer> called numberList. I want it to store unique integers and never allow duplicates to be added. I would like to do something like this:

private void add(int number) {
  if (!numberList.contains(number)) {
    numberList.add(number);
  }
}

But obviously this wont work because numberList contains a list of Integer objects so regardless of the number each is a unique object.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:04:15+00:00Added an answer on June 6, 2026 at 7:04 pm

    One is to store the Integers in a Set<Integer> such as a HashSet<Integer>. Sets do not allow duplicates.

    Edit
    Also, the Collection’s contains(...) method uses the object’s equals(…) method to see if it is held by the collection or not, so your method above will prevent duplicates as well, if you need to use a List as your collection. Test this out yourself and you’ll see it’s so.

    For instance:

      List<Integer> numberList = new ArrayList<Integer>();
      int[] myInts = {1, 1, 2, 3, 3, 3, 3, 4};
      for (int i : myInts) {
         if (!numberList.contains(i)) {
            numberList.add(i);
          }
      }
    
      System.out.println(numberList);
    

    will return: [1, 2, 3, 4]

    Also, one possible problem with HashSets is that they’re not ordered, so if ordering is important, you’ll want to look at using one of the other varieties of ordered Sets.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question Does anyone know of a way to round a float to the nearest
Question: I receive the following error for the code below, does anyone know why?
Question is not correctly framed i know, how do i code the below logic
Question mark I'm wondering whether it's possible for the VM guest machine to pop
Question: input an int number ex: ABCD, verify that is ABCD = AB*CD or
Question is : how to add any event in calender? i want to make
Question : Is there a simple way to print out an array in Ruby
Question: I'm looking for a way to simplify the construction of debugger type proxies
Question Is it possible to stop a Message Driven Bean (programmatically), so that it
Question context: let say that there is some really important row in config/locales/en.yml that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.