Find duplicate element in array in time O(n)

前端 未结 24 2588
余生分开走
余生分开走 2020-11-27 10:07

I have been asked this question in a job interview and I have been wondering about the right answer.

You have an array of numbers from 0 to n-1, one o

相关标签:
24条回答
  • 2020-11-27 11:07

    You could proceed as follows:

    1. sort your array by using a Linear-time sorting algorithm (e.g. Counting sort) - O(N)
    2. scan the sorted array and stop as soon as two consecutive elements are equal - O(N)
    0 讨论(0)
  • 2020-11-27 11:07
      public void duplicateNumberInArray {
        int a[] = new int[10];
        Scanner inp = new Scanner(System.in);
        for(int i=1;i<=5;i++){  
            System.out.println("enter no. ");
            a[i] = inp.nextInt();
        }
        Set<Integer> st = new HashSet<Integer>();
        Set<Integer> s = new HashSet<Integer>();
        for(int i=1;i<=5;i++){          
            if(!st.add(a[i])){
                s.add(a[i]);
            }
        }
    
        Iterator<Integer> itr = s.iterator();
                    System.out.println("Duplicate numbers are");
        while(itr.hasNext()){
            System.out.println(itr.next());
        }
    }
    

    First of all creating an array of integer using Scanner class. Then iterating a loop through the numbers and checking if the number can be added to set (Numbers can be added to set only when that particular number should not be in set already, means set does not allow duplicate no. to add and return a boolean vale FALSE on adding duplicate value).If no. cannot be added means it is duplicate so add that duplicate number into another set, so that we can print later. Please note onething that we are adding the duplicate number into a set because it might be possible that duplicate number might be repeated several times, hence add it only once.At last we are printing set using Iterator.

    0 讨论(0)
  • 2020-11-27 11:07

    Traverse through the array and check the sign of array[abs(array[i])], if positive make it as negative and if it is negative then print it, as follows:

    import static java.lang.Math.abs;
    
    public class FindRepeatedNumber {
    
        private static void findRepeatedNumber(int arr[]) {
            int i;
            for (i = 0; i < arr.length; i++) {
                if (arr[abs(arr[i])] > 0)
                    arr[abs(arr[i])] = -arr[abs(arr[i])];
                else {
                    System.out.print(abs(arr[i]) + ",");
                }
            }
        }
    
        public static void main(String[] args) {
            int arr[] = { 4, 2, 4, 5, 2, 3, 1 };
            findRepeatedNumber(arr);
        }
    }
    

    Reference: http://www.geeksforgeeks.org/find-duplicates-in-on-time-and-constant-extra-space/

    0 讨论(0)
  • 2020-11-27 11:09

    //This is similar to the HashSet approach but uses only one data structure:

        int[] a = { 1, 4, 6, 7, 4, 6, 5, 22, 33, 44, 11, 5 };
    
        LinkedHashMap<Integer, Integer> map = new LinkedHashMap<Integer, Integer>();
    
        for (int i : a) {
            map.put(i, map.containsKey(i) ? (map.get(i)) + 1 : 1);
        }
    
        Set<Entry<Integer, Integer>> es = map.entrySet();
        Iterator<Entry<Integer, Integer>> it = es.iterator();
    
        while (it.hasNext()) {
            Entry<Integer, Integer> e = it.next();
            if (e.getValue() > 1) {
                System.out.println("Dupe " + e.getKey());
            }
        }
    
    0 讨论(0)
  • 2020-11-27 11:10

    Use hashtable. Including an element in a hashtable is O(1).

    0 讨论(0)
  • 2020-11-27 11:12
    int a[] = {2,1,2,3,4};
    
    int b[] = {0};
    
    for(int i = 0; i < a.size; i++)
    {
    
        if(a[i] == a[i+1])
        {
             //duplicate found
             //copy it to second array
            b[i] = a[i];
        }
    }
    
    0 讨论(0)
提交回复
热议问题