Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Remove Duplicates from Array in Python
Forum Updated to NodeBB v4.3 + New Features

Remove Duplicates from Array in Python

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 3 Posters 3.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aliviya
    wrote on 18 Aug 2023, 10:46 last edited by
    #1

    Hello everyone

    In Python, I'm working to find a solution to the duplicates in an array issue. Online, I have discovered a few solutions, but I'm not sure which is the most effective. The blog I referred to Remove Duplicates from Array in Python

    Using a set is one option. A data structure known as a set exclusively stores singular components. In order to generate an array without duplicate elements, we can first create a set of the array's items.

    def remove_duplicates(array):
      seen = set()
      new_array = []
      for element in array:
        if element not in seen:
          seen.add(element)
          new_array.append(element)
      return new_array
    

    Utilising a hash table is a further remedy. A data structure that associates keys with values is a hash table. To convert each element in the array into a different number, we can utilise a hash table. Then, using iteration, we may add elements to the new array from the hash table that have unique numbers alone.

    def remove_duplicates(array):
      seen = {}
      new_array = []
      for element in array:
        if element not in seen:
          seen[element] = 1
          new_array.append(element)
      return new_array
    

    Which approach is the most effective among these? Is there a more effective approach in Python to eliminate duplicates from an array?

    J 1 Reply Last reply 18 Aug 2023, 10:59
    0
    • A Aliviya
      18 Aug 2023, 10:46

      Hello everyone

      In Python, I'm working to find a solution to the duplicates in an array issue. Online, I have discovered a few solutions, but I'm not sure which is the most effective. The blog I referred to Remove Duplicates from Array in Python

      Using a set is one option. A data structure known as a set exclusively stores singular components. In order to generate an array without duplicate elements, we can first create a set of the array's items.

      def remove_duplicates(array):
        seen = set()
        new_array = []
        for element in array:
          if element not in seen:
            seen.add(element)
            new_array.append(element)
        return new_array
      

      Utilising a hash table is a further remedy. A data structure that associates keys with values is a hash table. To convert each element in the array into a different number, we can utilise a hash table. Then, using iteration, we may add elements to the new array from the hash table that have unique numbers alone.

      def remove_duplicates(array):
        seen = {}
        new_array = []
        for element in array:
          if element not in seen:
            seen[element] = 1
            new_array.append(element)
        return new_array
      

      Which approach is the most effective among these? Is there a more effective approach in Python to eliminate duplicates from an array?

      J Offline
      J Offline
      JonB
      wrote on 18 Aug 2023, 10:59 last edited by
      #2

      @Aliviya
      Hi. This forum is not necessarily the best for generic Python questions.

      Either of your techniques is standard. set() might be the most efficient.

      If you are using numpy it offers a NumPy: numpy.unique() function to save you writing the code.

      1 Reply Last reply
      2
      • A Offline
        A Offline
        alan23
        wrote on 29 Aug 2023, 06:12 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved