Find a single integer that occurs with even frequency in a given array of ints when all others occur odd with frequency

后端 未结 5 1371
盖世英雄少女心
盖世英雄少女心 2021-02-03 10:26

This is an interview question.

Given an array of integers, find the single integer value in the array which occurs with even frequency. All integers will be

相关标签:
5条回答
  • 2021-02-03 10:47

    Scan through the list maintaining two sets, the 'Even' set and the 'Odd' set. If an element hasn't been seen before (i.e. if it's in neither set), place it in the 'Odd' set. If an element is in one set, move it to the other set. At the end, there should be only one item in the 'Even' set. This probably won't be fast, but the memory usage should be reasonable for large lists.

    0 讨论(0)
  • 2021-02-03 10:48

    Given this is an interview question, the answer is: O(1) space is achievable "for very big values of 1":

    • Prepare a matcharray 1..INT_MAX of all 0
    • When traversing the array, use the integer as an index into the matcharray, adding 1
    • When done, traverse the match array to find the one entry with a positive even value

    The space for this is large, but independent of the size of the input array, so O(1) space. For really big data sets (say small value range, but enormous array length), this might even be a practically valid solution.

    0 讨论(0)
  • 2021-02-03 10:49

    If you are allowed to sort the original array, I believe that you can do this in O(n lg U) time and O(lg U) space, where U is the maximum element of the array. The idea is as follows - using in-place MSD radix sort, sort the array in O(n lg U) time and O(lg U) space. Then, iterate across the array. Since all equal values are consecutive, you can then count how many times each value appears. Once you find the value that appears an even number of times, you can output the answer. This second scan requires O(n) time and O(1) space.

    If we assume that U is a fixed constant, this gives an O(n)-time, O(1)-space algorithm. If you don't assume this, then the memory usage is still better than the O(n) algorithm provided that lg U = O(n), which should be true on most machines. Moreover, the space usage is only logarithmically as large as the largest element, meaning that the practical space usage is quite good. For example, on a 64-bit machine, we'd need only space sufficient to hold 64 recursive calls. This is much better than allocating a gigantic array up-front. Moreover, it means that the algorithm is a weakly-polynomial time algorithm as a function of U.

    That said, this does rearrange the original array, and thus does destructively modify the input. In a sense, it's cheating because it uses the array itself for the O(n) storage space.

    Hope this helps!

    0 讨论(0)
  • 2021-02-03 10:59

    I guess we read the task improperly. It asks us "find the single integer value in the array which occurs with even frequency". So, assuming that there is exactly ONE even element, the solution is:

    public static void main(String[] args) {
        int[] array = { 2, 1, 2, 4, 4 };
    
        int count = 0;
        for (int i : array) {
            count^=i;
        }
        System.out.println(count); // Prints 1
    }
    
    0 讨论(0)
  • 2021-02-03 11:02

    -Make a hash table containing ints. Call it is_odd or something. Since you might have to look through an array of size INT_MAX, just make it an array of size INT_MAX. Initialize to 0.

    -Traverse through the whole array. You have to do this. There's no way to beat O(n).

    for each number:
      if it's not in the hash table, mark its spot in the table as 1.
    
      if it is in the hash table then:
        if its value is '1', make it '2'
        if its value is '2', make it '1'.
    

    Now you have to traverse through the hash table. Pull out the sole entry with "2" as the value.

    Time: You traverse the array once and the hash table once, so O(n).

    Space: Just an array of size INT_MAX. Or if you know the range of your array you can restrict your memory use to that.

    edit: I just saw that you already had this method. Sorry about that!

    0 讨论(0)
提交回复
热议问题