1207. Unique Number of Occurrences
Il testo del problema è tradotto dal russo per la lingua selezionata. Il codice resta invariato.
Дан array целых чисел arr. return true, если количество вхождений каждого значения в arrayе уникально, или false в противном случае.
Esempio:
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.
C# soluzione
abbinato/originaleusing System;
using System.Collections.Generic;
public class Solution {
public bool UniqueOccurrences(int[] arr) {
Dictionary<int, int> freq = new Dictionary<int, int>();
foreach (int num in arr) {
if (freq.ContainsKey(num)) {
freq[num]++;
} else {
freq[num] = 1;
}
}
HashSet<int> freqSet = new HashSet<int>(freq.Values);
return freq.Count == freqSet.Count;
}
}
C++ soluzione
bozza automatica, rivedere prima dell'invio#include <bits/stdc++.h>
using namespace std;
// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
public bool UniqueOccurrences(vector<int>& arr) {
unordered_map<int, int> freq = new unordered_map<int, int>();
foreach (int num in arr) {
if (freq.count(num)) {
freq[num]++;
} else {
freq[num] = 1;
}
}
HashSet<int> freqSet = new HashSet<int>(freq.Values);
return freq.size() == freqSet.size();
}
}
Java soluzione
abbinato/originaleclass Solution {
public boolean uniqueOccurrences(int[] arr) {
Map<Integer, Integer> freq = new HashMap<>();
for (int num : arr) {
freq.put(num, freq.getOrDefault(num, 0) + 1);
}
Set<Integer> freqSet = new HashSet<>(freq.values());
return freq.size() == freqSet.size();
}
}
JavaScript soluzione
abbinato/originalevar uniqueOccurrences = function(arr) {
let freq = new Map();
for (let num of arr) {
freq.set(num, (freq.get(num) || 0) + 1);
}
let freqSet = new Set(freq.values());
return freq.size === freqSet.size;
};
Python soluzione
abbinato/originaleclass Solution:
def uniqueOccurrences(self, arr: List[int]) -> bool:
freq = {}
for num in arr:
freq[num] = freq.get(num, 0) + 1
return len(freq) == len(set(freq.values()))
Go soluzione
abbinato/originalefunc uniqueOccurrences(arr []int) bool {
freq := make(map[int]int)
for _, num := range arr {
freq[num]++
}
freqSet := make(map[int]struct{})
for _, count := range freq {
freqSet[count] = struct{}{}
}
return len(freq) == len(freqSet)
}
Algorithm
1⃣Сохраните частоты elementов arrayа arr в хэш-таблице freq.
2⃣Итерируйтесь по хэш-таблице freq и вставьте частоты всех уникальных elementов arrayа arr в хэш-набор freqSet.
3⃣return true, если размер хэш-набора freqSet равен размеру хэш-таблицы freq, иначе return false.
😎
Vacancies for this task
offerte attive with overlapping task tags are mostrati.
Non ci sono ancora offerte attive.