How to efficiently remove duplicates from an array without using Set

后端 未结 30 2521
情深已故
情深已故 2020-11-22 07:29

I was asked to write my own implementation to remove duplicated values in an array. Here is what I have created. But after tests with 1,000,000 elements it took very long ti

相关标签:
30条回答
  • 2020-11-22 07:32
     package javaa;
    
    public class UniqueElementinAnArray 
    {
    
     public static void main(String[] args) 
     {
        int[] a = {10,10,10,10,10,100};
        int[] output = new int[a.length];
        int count = 0;
        int num = 0;
    
        //Iterate over an array
        for(int i=0; i<a.length; i++)
        {
            num=a[i];
            boolean flag = check(output,num);
            if(flag==false)
            {
                output[count]=num;
                ++count;
            }
    
        }
    
        //print the all the elements from an array except zero's (0)
        for (int i : output) 
        {
            if(i!=0 )
                System.out.print(i+"  ");
        }
    
    }
    
    /***
     * If a next number from an array is already exists in unique array then return true else false
     * @param arr   Unique number array. Initially this array is an empty.
     * @param num   Number to be search in unique array. Whether it is duplicate or unique.
     * @return  true: If a number is already exists in an array else false 
     */
    public static boolean check(int[] arr, int num)
    {
        boolean flag = false;
        for(int i=0;i<arr.length; i++)
        {
            if(arr[i]==num)
            {
                flag = true;
                break;
            }
        }
        return flag;
    }
    

    }

    0 讨论(0)
  • 2020-11-22 07:33

    This is simple way to sort the elements in the array

    public class DublicatesRemove {
        public static void main(String args[]) throws Exception {
    
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("enter size of the array");
            int l = Integer.parseInt(br.readLine());
            int[] a = new int[l];
            // insert elements in the array logic
            for (int i = 0; i < l; i++) 
            {
                System.out.println("enter a element");
                int el = Integer.parseInt(br.readLine());
                a[i] = el;
            }
            // sorting elements in the array logic
            for (int i = 0; i < l; i++) 
            {
                for (int j = 0; j < l - 1; j++) 
                {
                    if (a[j] > a[j + 1])
                    {
                        int temp = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = temp;
                    }
                }
            }
            // remove duplicate elements logic
            int b = 0;
            a[b] = a[0];
            for (int i = 1; i < l; i++)
            {
                if (a[b] != a[i])
                {
                    b++;
                    a[b]=a[i];
    
                }
    
            }
            for(int i=0;i<=b;i++)
            {
                System.out.println(a[i]);
            }
    
    
        }
    }
    
    0 讨论(0)
  • 2020-11-22 07:33

    Why do all people not check this below lines?

    I need to write my own implementation - not to use Set, HashSet etc. Or any other tools such as iterators. Simply an array to remove duplicates.

    I'm posting very simple implementation with caring the above line.

    public class RemoveDuplicates {
    
    public static void main(String[] args) {
    
        int[] arr = { 1, 2, 3, 4, 2, 3, 1 }; // input array
        int len = arr.length;
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < len; j++) {
                if (arr[i] == arr[j]) {
                    while (j < (len) - 1) {
                        arr[j] = arr[j - 1];
                        j++;
                    }
                    len--;
                }
            }
        }
        for (int i = 0; i < len; i++) {
            System.out.print("  " +arr[i]);
        }
    
       }
     }
    

    Input : 1, 2, 3, 4, 2, 3, 1

    Output : 1 2 3 4

    0 讨论(0)
  • 2020-11-22 07:35

    There exists many solution of this problem.

    1. The sort approach

      • You sort your array and resolve only unique items
    2. The set approach

      • You declare a HashSet where you put all item then you have only unique ones.
    3. You create a boolean array that represent the items all ready returned, (this depend on your data in the array).

    If you deal with large amount of data i would pick the 1. solution. As you do not allocate additional memory and sorting is quite fast. For small set of data the complexity would be n^2 but for large i will be n log n.

    0 讨论(0)
  • 2020-11-22 07:35

    What if you create two boolean arrays: 1 for negative values and 1 for positive values and init it all on false.

    Then you cycle thorugh the input array and lookup in the arrays if you've encoutered the value already. If not, you add it to the output array and mark it as already used.

    0 讨论(0)
  • 2020-11-22 07:36
    class Demo 
    {
        public static void main(String[] args) 
        {
            int a[]={3,2,1,4,2,1};
            System.out.print("Before Sorting:");
            for (int i=0;i<a.length; i++ )
            {
                System.out.print(a[i]+"\t");
            }
            System.out.print ("\nAfter Sorting:");
            //sorting the elements
            for(int i=0;i<a.length;i++)
            {
                for(int j=i;j<a.length;j++)
                {
                    if(a[i]>a[j])
                    {
                        int temp=a[i];
                        a[i]=a[j];
                        a[j]=temp;
                    }
    
                }
            }
    
            //After sorting
            for(int i=0;i<a.length;i++)
            {
                System.out.print(a[i]+"\t");
            }
            System.out.print("\nAfter removing duplicates:");
            int b=0;
            a[b]=a[0];
            for(int i=0;i<a.length;i++)
            {
                if (a[b]!=a[i])
                {
                    b++;
                    a[b]=a[i];
                }
            }
            for (int i=0;i<=b;i++ )
            {
                System.out.print(a[i]+"\t");
            }
        }
    }
      OUTPUT:Before Sortng:3 2 1 4 2 1 After Sorting:1 1 2 2 3 4 
                    Removing Duplicates:1 2 3 4
    
    0 讨论(0)
提交回复
热议问题