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

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

提交回复
热议问题