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

后端 未结 5 1410
盖世英雄少女心
盖世英雄少女心 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: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.

提交回复
热议问题