diff --git a/README.md b/README.md index 96cdfa1..8839860 100644 --- a/README.md +++ b/README.md @@ -303,6 +303,10 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重 - LeetCode 152. 乘积最大子数组 - LintCode 191. 乘积最大子序列 +- [最高频率的IP](src/array/highest-frequency-ip.js) + + - LintCode 1613. 最高频率的IP + ## 栈 - [最大矩阵](src/stack/maximal-rectangle.js) diff --git a/src/array/highest-frequency-ip.js b/src/array/highest-frequency-ip.js new file mode 100644 index 0000000..888f435 --- /dev/null +++ b/src/array/highest-frequency-ip.js @@ -0,0 +1,19 @@ +/** + * @param ipLines: ip address + * @return: return highestFrequency ip address + */ +export const highestFrequency = function (ipLines) { + const map = new Map() + let max = 0 + let maxN = ipLines[0] + ipLines.forEach(n => { + const temp = (map.get(n) || 0) + 1 + map.set(n, temp) + if (temp > max) { + maxN = n + max = temp + } + }) + + return maxN +} diff --git a/test/array/highest-frequency-ip.test.js b/test/array/highest-frequency-ip.test.js new file mode 100644 index 0000000..ddbcc9a --- /dev/null +++ b/test/array/highest-frequency-ip.test.js @@ -0,0 +1,6 @@ +import { highestFrequency } from '../../src/array/highest-frequency-ip' + +test('最高频率的IP', () => { + expect(highestFrequency(['192.168.1.1', '192.118.2.1', '192.168.1.1'])).toEqual('192.168.1.1') + expect(highestFrequency(['192.168.1.1', '192.118.2.1', '192.168.1.1', '192.118.2.1', '192.118.2.1'])).toEqual('192.118.2.1') +})