Array remove duplicate elements

前端 未结 12 1324
陌清茗
陌清茗 2020-11-27 16:17

I have an unsorted array, what is the best method to remove all the duplicates of an element if present?

e.g:

a[1,5,2,6,8,9,1,1,10,3,2,4,1,3,11,3]


        
相关标签:
12条回答
  • 2020-11-27 16:37

    Treat numbers as keys.

    for each elem in array:
    if hash(elem) == 1 //duplicate
      ignore it
      next
    else
      hash(elem) = 1
      add this to resulting array 
    end
    
    If you know about the data like the range of numbers and if it is finite, then you can initialize that big array with ZERO's.
    array flag[N] //N is the max number in the array
    for each elem in input array:
      if flag[elem - 1] == 0
        flag[elem - 1] = 1
        add it to resulatant array
      else
        discard it //duplicate
      end
    

    0 讨论(0)
  • 2020-11-27 16:37

    You can use the "in" and "not in" syntax in python which makes it pretty straight forward.

    The complexity is higher than the hashing approach though since a "not in" is equivalent to a linear traversal to find out whether that entry exists or not.

    li = map(int, raw_input().split(","))
    a = []
    for i in li:
        if i not in a:
            a.append(i)
    print a
    
    0 讨论(0)
  • 2020-11-27 16:41

    If you don't need to keep the original object you can loop it and create a new array of unique values. In C# use a List to get access to the required functionality. It's not the most attractive or intelligent solution, but it works.

    int[] numbers = new int[] {1,2,3,4,5,1,2,2,2,3,4,5,5,5,5,4,3,2,3,4,5};
    List<int> unique = new List<int>();
    
    foreach (int i in numbers)
         if (!unique.Contains(i))
              unique.Add(i);
    
    unique.Sort();
    numbers = unique.ToArray();
    
    0 讨论(0)
  • 2020-11-27 16:41
        indexOutput = 1;
        outputArray[0] = arrayInt[0];
        int j;
        for (int i = 1; i < arrayInt.length; i++) {            
            j = 0;
            while ((outputArray[j] != arrayInt[i]) && j < indexOutput) {
                j++;
            }
            if(j == indexOutput){
               outputArray[indexOutput] = arrayInt[i];
               indexOutput++;
            }         
        }
    
    0 讨论(0)
  • 2020-11-27 16:41

    This is a code segment i created in C++, Try out it

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
       cout << " Delete the duplicate" << endl; 
    
       int numberOfLoop = 10;
       int loopCount =0;
       int indexOfLargeNumber = 0;
       int largeValue = 0;
       int indexOutput = 1;
    
       //Array to hold the numbers
       int arrayInt[10] = {};
       int outputArray [10] = {};
    
       // Loop for reading the numbers from the user input
       while(loopCount < numberOfLoop){       
           cout << "Please enter one Integer number" << endl;
           cin  >> arrayInt[loopCount];
           loopCount = loopCount + 1;
       }
    
    
    
        outputArray[0] = arrayInt[0];
        int j;
        for (int i = 1; i < numberOfLoop; i++) {            
            j = 0;
            while ((outputArray[j] != arrayInt[i]) && j < indexOutput) {
                j++;
            }
            if(j == indexOutput){
               outputArray[indexOutput] = arrayInt[i];
               indexOutput++;
            }         
        }
    
       cout << "Printing the Non duplicate array"<< endl;
    
       //Reset the loop count
       loopCount =0;
    
       while(loopCount < numberOfLoop){ 
           if(outputArray[loopCount] != 0){
            cout <<  outputArray[loopCount] << endl;
        }     
    
           loopCount = loopCount + 1;
       }   
       return 0;
    }
    
    0 讨论(0)
  • 2020-11-27 16:43
    Time O(n) space O(n) 
    
    #include <iostream>
        #include<limits.h>
        using namespace std;
        void fun(int arr[],int size){
    
            int count=0;
            int has[100]={0};
            for(int i=0;i<size;i++){
                if(!has[arr[i]]){
                   arr[count++]=arr[i];
                   has[arr[i]]=1;
                }
            }
         for(int i=0;i<count;i++)
           cout<<arr[i]<<" ";
        }
    
        int main()
        {
            //cout << "Hello World!" << endl;
            int arr[]={4, 8, 4, 1, 1, 2, 9};
            int size=sizeof(arr)/sizeof(arr[0]);
            fun(arr,size);
    
            return 0;
        }
    
    0 讨论(0)
提交回复
热议问题