Algorithms to Determine Unique Number of Occurrences

  • 时间:2020-09-18 17:39:21
  • 分类:网络文摘
  • 阅读:122 次

Given an array of integers arr, write a function that returns true if and only if the number of occurrences of each value in the array is unique.

Example 1:
Input: arr = [1,2,2,1,1,3]
Output: true
Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.

Example 2:
Input: arr = [1,2]
Output: false

Example 3:
Input: arr = [-3,0,1,-3,1,1,1,-3,10,0]
Output: true

Constraints:
1 <= arr.length <= 1000
-1000 <= arr[i] <= 1000

Using Hashmap and Hashset

We can use hashmap e.g. the unordered_map in C++ to record the number of occurencies for the numbers. Then we can use a hash set to determine if an occurence has appeared or not – return false immediately once we found at least one occurence is not unique.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
    bool uniqueOccurrences(vector<int>& arr) {
        unordered_map<int, int> data;
        for (const auto &n: arr) {
            data[n] ++;
        }
        unordered_set<int> hash;
        for (auto it = data.begin(); it != data.end(); it ++) {
            if (hash.count(it->second)) {
                return false;
            }
            hash.insert(it->second);
        }
        return true;
    }
};
class Solution {
public:
    bool uniqueOccurrences(vector<int>& arr) {
        unordered_map<int, int> data;
        for (const auto &n: arr) {
            data[n] ++;
        }
        unordered_set<int> hash;
        for (auto it = data.begin(); it != data.end(); it ++) {
            if (hash.count(it->second)) {
                return false;
            }
            hash.insert(it->second);
        }
        return true;
    }
};

Alternatively, we can push all the occurrences values into the set and compare the sizes of both hash map and hash set.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
    bool uniqueOccurrences(vector<int>& arr) {
        unordered_map<int, int> data;
        for (const auto &n: arr) {
            data[n] ++;
        }
        unordered_set<int> hash;
        for (auto it = data.begin(); it != data.end(); it ++) {
            hash.insert(it->second);
        }
        return hash.size() == data.size();
    }
};
class Solution {
public:
    bool uniqueOccurrences(vector<int>& arr) {
        unordered_map<int, int> data;
        for (const auto &n: arr) {
            data[n] ++;
        }
        unordered_set<int> hash;
        for (auto it = data.begin(); it != data.end(); it ++) {
            hash.insert(it->second);
        }
        return hash.size() == data.size();
    }
};

Apparently, both algorithms are O(N) time and O(N) space. The first approach may be slightly faster due to early exit while the second implementation looks concise.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
为 wordpress 文章作者在评论留言时显示“本文作者”提示  为 WordPress 主题添加花瓣飘落特效  wordpress插件:WP-China-Yes 切换WP站点与官方通信至国内节点解决后台更新429错误  一段代码轻松解决wordpress定时发布失败的问题  WordPress官网打不开 出现 429 Too Many Request 的原因  下载更新wordpress程序及插件的方法  禁用wordpress4.4+版本自动生成768w像素缩略图功能  自动为wordpress文章图片添加alt属性和title属性  如何为WordPress导航菜单、标签、出站等链接添加nofollow标签属性  如何设置WordPress的RSS feed更新频率 
评论列表
添加评论