1207. Unique Number of Occurrences

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

    
    
    
    class Solution {
        public boolean uniqueOccurrences(int[] arr) {
            Arrays.sort(arr);
            HashMap<Integer, Integer> map = new HashMap();
            for(int i: arr){
                map.put(i, 1 + map.getOrDefault(i, 0));
            }
            ArrayList<Integer> list = new ArrayList(map.values());
            Collections.sort(list);
            for(int i = 1; i < list.size(); i++){
                if(list.get(i) == list.get(i-1)) return false;
            }
            return true;
        }
    }
    
    
     

    绕远路还是我会绕。

HashMap的values() 方法返回一个values的set,形式为collections,所以可以转化为list

原文地址:https://www.cnblogs.com/wentiliangkaihua/p/11624005.html

时间: 2024-10-12 18:00:08

1207. Unique Number of Occurrences的相关文章

【leetcode】1207. Unique Number of Occurrences

题目如下: 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 h

LoadRunner压力测试之Unique Number参数类型、Random Number参数类型浅析

前几天工作需要用LoadRunner进行压力测试,期间对手机号进行参数化设置. 当时选用了<Value>137{Random_quhao}{Unique_weiyi}</Value>模式,Random Number.Unque Number参数类型选择如下: Parameter type Random Number Unique Number Number(Block size per) 9999 9999 Sample 0001 0001 Number %04lu %04d Up

mysql生成不重复随机数(unique number generation)

转自:http://blog.csdn.net/dreamer2020/article/details/52049629 问题来源 业务中有时会遇到要生成不重复随机数的情况,例如,新生成一个商品编号.房间编号.或者其他物品编号等.不愿意采用表的自增索引 id,同时又希望新生成的编号是不重复的. 这就需要考验mysql的随机数功能了. Solution mysql的rand函数可以生成一个0到1之间的随机数,进行一定的放大即可得到一个随机数.再通过条件查询来限制新随机数没有在表中出现过.如下所示:

Count the number of occurrences in a sorted array

Given a sorted array arr[] and a number x, write a function that counts the occurrences of x in arr[]. Expected time complexity is O(Logn) Examples: Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 2 Output: 4 // x (or 2) occurs 4 times in arr[] Input: arr

LoadRunner 中的 Unique Number 参数类型小结

How to count the occurrences of a number?

I have a file, consisting of a column of numbers, some of which are the same, I want to count the occurrences of each unique number, here is the simple way: cat filename | sort | uniq -c Be sure that the numbers are in a column.

*Find the Number Occurring Odd Number of Times

Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Find the number in O(n) time & constant space. Example:I/P = [1, 2, 3, 2, 3, 1, 3]O/P = 3 A Simple Solution is to run two

1041. Be Unique (20)【水题】——PAT (Advanced Level) Practise

题目信息 1041. Be Unique (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1, 104]. The first on

pat1041. Be Unique (20)

1041. Be Unique (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen fr