param { number [ ] } numbers return { number } function MoreThanHalfNum ( numbers ) { if ( ! numbers && numbers . length < 1 ) { return 0 ; } else { let length = numbers . length ; let count = { } ; for ( let i = 0 ; i < length ; i ++ ) { if ( count [ 'the' + numbers [ i ] ] ) { count [ 'the' + numbers [ i ] ] ++ ; } else { count [ 'the' + numbers [ i ] ] = 1 ; } if ( count [ 'the' + numbers [ i ] ] > length / 2 ) { return numbers [ i ] ; } } return 0 ; } } param { number [ ] } numbers return { number } function MoreThanHalfNum2 ( numbers ) { if ( ! numbers && numbers . length < 1 ) { return 0 ; } else { let len = numbers . length ; let result = numbers [ 0 ] ; let times = 1 ; for ( let i = 1 ; i < len ; i ++ ) { if ( times === 0 ) { result = numbers [ i ] ; times = 1 ; } else if ( numbers [ i ] === result ) { times ++ ; } else { times -- ; } } return result ; } } export { MoreThanHalfNum , MoreThanHalfNum2 } ;