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 7707207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:18:05+00:00 2026-06-01T00:18:05+00:00

How do I find the duplicates in a list of integers and create another

  • 0

How do I find the duplicates in a list of integers and create another list of the duplicates?

  • 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-01T00:18:06+00:00Added an answer on June 1, 2026 at 12:18 am

    To remove duplicates use set(a). To print duplicates, something like:

    a = [1,2,3,2,1,5,6,5,5,5]
    
    import collections
    print([item for item, count in collections.Counter(a).items() if count > 1])
    
    ## [1, 2, 5]
    

    Note that Counter is not particularly efficient (timings) and probably overkill here. set will perform better. This code computes a list of unique elements in the source order:

    seen = set()
    uniq = []
    for x in a:
        if x not in seen:
            uniq.append(x)
            seen.add(x)
    

    or, more concisely:

    seen = set()
    uniq = [x for x in a if x not in seen and not seen.add(x)]    
    

    I don’t recommend the latter style, because it is not obvious what not seen.add(x) is doing (the set add() method always returns None, hence the need for not).

    To compute the list of duplicated elements without libraries:

    seen = set()
    dupes = []
    
    for x in a:
        if x in seen:
            dupes.append(x)
        else:
            seen.add(x)
    

    or, more concisely:

    seen = set()
    dupes = [x for x in a if x in seen or seen.add(x)]    
    

    If list elements are not hashable, you cannot use sets/dicts and have to resort to a quadratic time solution (compare each with each). For example:

    a = [[1], [2], [3], [1], [5], [3]]
    
    no_dupes = [x for n, x in enumerate(a) if x not in a[:n]]
    print no_dupes # [[1], [2], [3], [5]]
    
    dupes = [x for n, x in enumerate(a) if x in a[:n]]
    print dupes # [[1], [3]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to find if a list contains duplicates. For example: list1
Possible Duplicates: find whether a loop in a linked list without two pointers How
I've got a List<string> that contains duplicates and I need to find the indexes
Is there a direct way to find out if the list contains duplicates? Direct
Is there a easy way to find duplicates in list, and then sort them
How can i find the count of duplicates in this list. >>> result =
I am trying to find duplicates in relationship and omit them from my list.
It's easy to find duplicates with one field: SELECT email, COUNT(email) FROM users GROUP
I wrote two queries to find duplicates in the array var groups = from
This is probably a Microsoft interview question. Find the kth smallest element (ignoring duplicates)

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.