Leetcode 1207. Unique Number of Occurrences

admin2024-09-01  9

Problem

Given an array of integers arr, return true if the number of occurrences of each value in the array is unique or false otherwise.

Algorithm

Sort the array and test the adjoint numbers.

Code

class Solution:
    def uniqueOccurrences(self, arr: List[int]) -> bool:
        cnts = [0] * 1001
        for a in arr:
            cnts[a] += 1
        cnts.sort(reverse=True)
        
        index = 1
        while index < 1001 and cnts[index]:
            if cnts[index] == cnts[index-1]:
                return False
            index += 1
        return True
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明原文出处。如若内容造成侵权/违法违规/事实不符,请联系SD编程学习网:675289112@qq.com进行投诉反馈,一经查实,立即删除!