Unique Number of Occurrences

🏠 ⬅️ ➡️

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

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

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: n = 10
    integer, dimension(n) :: arr
    logical :: result

    ! Example 1
    arr = [1, 2, 2, 1, 1, 3]
    write (*, '(A)') 'Example 1:'
    write (*, '(A)') 'Input:  ', arr
    result = uniqueOccurrences(arr)
    write (*, '(A, L1)') 'Output: ', result
    write (*, *)

    ! Example 2
    arr = [1, 2]
    write (*, '(A)') 'Example 2:'
    write (*, '(A)') 'Input:  ', arr
    result = uniqueOccurrences(arr)
    write (*, '(A, L1)') 'Output: ', result
    write (*, *)

    ! Example 3
    arr = [-3, 0, 1, -3, 1, 1, 1, -3, 10, 0]
    write (*, '(A)') 'Example 3:'
    write (*, '(A)') 'Input:  ', arr
    result = uniqueOccurrences(arr)
    write (*, '(A, L1)') 'Output: ', result
    write (*, *)

contains

    function uniqueOccurrences(arr) result(result_)
        implicit none
        integer, dimension(:), intent(in) :: arr
        logical :: result_

        integer :: i, j
        integer :: count

        do i = 1, size(arr) - 1
            count = 0
            do j = i + 1, size(arr)
                if (arr(i) == arr(j)) then
                    count = count + 1
                end if
            end do
            if (count > 0) then
                result_ = .false.
                return
            end if
        end do

        result_ = .true.
    end function uniqueOccurrences

end program main
Compiled
Executed
Correct
module UniqueOccurrences

implicit none

contains

function uniqueOccurrences(arr) result(output)

integer, intent(in) :: arr(:)
logical :: output

integer :: i, count

output = .true.

do i = 1, size(arr)

count = countOccurrences(arr, arr(i))

if (count > 1) then

output = .false.

exit

end if

end do

end function uniqueOccurrences

function countOccurrences(arr, value) result(count)

integer, intent(in) :: arr(:), value
integer :: count

count = count(arr == value)

end function countOccurrences

end module UniqueOccurrences

program test

use UniqueOccurrences

implicit none

integer, parameter :: N = 10
integer :: arr(N)

arr = [1, 2, 2, 1, 1, 3, -1, -1, -1, -1]

if (uniqueOccurrences(arr)) then

print *, "Unique occurrences"

else

print *, "Non-unique occurrences"

end if

arr = [1, 2]

if (uniqueOccurrences(arr)) then

print *, "Unique occurrences"

else

print *, "Non-unique occurrences"

end if

arr = [-3, 0, 1, -3, 1, 1, 1, -3, 10, 0]

if (uniqueOccurrences(arr)) then

print *, "Unique occurrences"

else

print *, "Non-unique occurrences"

end if

end program test
🌐 Data from online sources
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

def delNodes(root, to_delete):
    forest = []
    to_delete_set = set(to_delete)
    helper(root, True, to_delete_set, forest)
    return forest

def helper(node, isRoot, to_delete, forest):
    if not node:
        return None
    deleted = node.val in to_delete
    if isRoot and not deleted:
        forest.append(node)
    node.left = helper(node.left, deleted, to_delete, forest)
    node.right = helper(node.right, deleted, to_delete, forest)
    return None if deleted else node
The algorithm performs a post-order traversal, visiting each node of the tree. It starts with the root of the tree and checks whether it's a root node or not. If it's a root node, it adds the node to the forest; otherwise, it continues traversal. Left and right children are updated for each node. If a node's value is found in the `to_delete` list, this function returns nullptr (Python: None, JavaScript: null, Java: null) otherwise, it returns the current node. This way, the tree nodes will be combined with their new parents after the deletion, and the forest will have the roots of the disjoint trees.
🌐 Data from online sources
#include <vector>
#include <set>
#include <memory>

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

std::vector<TreeNode*> delNodes(TreeNode* root, std::vector<int>& to_delete) {
    std::vector<TreeNode*> forest;
    std::set<int> toDelete(to_delete.begin(), to_delete.end());
    helper(root, true, toDelete, forest);
    return forest;
}

TreeNode* helper(TreeNode* node, bool isRoot, std::set<int>& toDelete, std::vector<TreeNode*>& forest) {
    if (!node) return nullptr;
    bool deleted = toDelete.count(node->val);
    if (isRoot && !deleted) forest.push_back(node);
    node->left = helper(node->left, deleted, toDelete, forest);
    node->right = helper(node->right, deleted, toDelete, forest);
    return deleted ? nullptr : node;
}
The algorithm performs a post-order traversal, visiting each node of the tree. It starts with the root of the tree and checks whether it's a root node or not. If it's a root node, it adds the node to the forest; otherwise, it continues traversal. Left and right children are updated for each node. If a node's value is found in the `to_delete` list, this function returns nullptr (Python: None, JavaScript: null, Java: null) otherwise, it returns the current node. This way, the tree nodes will be combined with their new parents after the deletion, and the forest will have the roots of the disjoint trees.