Skip to content

Commit 269daad

Browse files
committed
leetcode
1 parent 15aa2f8 commit 269daad

File tree

4 files changed

+254
-0
lines changed

4 files changed

+254
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
151151
- [**446.** Arithmetic Slices II - Subsequence](ArithmeticSlicesII-Subsequence/arithmetic_slices_II_subsequence.dart)
152152
- [**2225.** Find Players With Zero or One Losses](FindPlayersWithZeroOrOneLosses/find_players_with_zero_or_one_losses.dart)
153153
- [**380.** Insert Delete GetRandom O(1)](InsertDeleteGetRandom-01/insert_delete_getrandom_O1.dart)
154+
- [**1207.** Unique Number of Occurrences](UniqueNumberOfOccurrences/unique_number_of_occurrences.dart)
154155

155156
## Reach me via
156157

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
3+
4+
-* 1207. Unique Number of Occurrences *-
5+
6+
7+
Given an array of integers arr, return true if the number of occurrences of each value in the array is unique, or false otherwise.
8+
9+
10+
11+
Example 1:
12+
13+
Input: arr = [1,2,2,1,1,3]
14+
Output: true
15+
Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.
16+
Example 2:
17+
18+
Input: arr = [1,2]
19+
Output: false
20+
Example 3:
21+
22+
Input: arr = [-3,0,1,-3,1,1,1,-3,10,0]
23+
Output: true
24+
25+
26+
Constraints:
27+
28+
1 <= arr.length <= 1000
29+
-1000 <= arr[i] <= 1000
30+
31+
32+
*/
33+
34+
import 'dart:collection';
35+
36+
class A {
37+
bool uniqueOccurrences(List<int> arr) {
38+
// hashmap to store all values of the array as key value pair
39+
HashMap<int, int> map = HashMap();
40+
// for all value inside our array
41+
for (int number in arr) {
42+
// we will add value of array as key and value + 1
43+
map[number] = (map[number] ?? 0) + 1;
44+
}
45+
// hashSet to see the unique values
46+
HashSet<int> hashSet = HashSet();
47+
// we will add all the values inside the set
48+
hashSet.addAll(map.values);
49+
// if the length of the unique set is same as the length of the map than true else false
50+
return hashSet.length == map.length;
51+
}
52+
}
53+
54+
class B {
55+
bool uniqueOccurrences(List<int> arr) {
56+
arr.sort();
57+
HashSet<int> unique = HashSet();
58+
59+
int c = 1;
60+
for (int i = 1; i < arr.length; i++) {
61+
if (arr[i] == arr[i - 1])
62+
c++;
63+
else {
64+
if (unique.contains(c)) return false;
65+
66+
unique.add(c);
67+
68+
c = 1;
69+
}
70+
}
71+
72+
if (unique.contains(c)) return false;
73+
74+
return true;
75+
}
76+
}
77+
78+
class C {
79+
bool uniqueOccurrences(List<int> arr) {
80+
HashMap<int, int> map = HashMap();
81+
82+
for (int i = 0; i < arr.length; i++) {
83+
int ele = arr[i];
84+
if (map.containsKey(ele)) {
85+
map[ele] = (map[ele] ?? 0) + 1;
86+
} else {
87+
map[ele] = 1;
88+
}
89+
}
90+
91+
List<int> freq = List.filled(1000, 0); //Constraint given
92+
93+
for (MapEntry<int, int> e in map.entries) {
94+
int n = e.value;
95+
if (freq[n] != 0) {
96+
return false;
97+
}
98+
freq[n]++;
99+
}
100+
101+
return true;
102+
}
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
/*
4+
5+
bool uniqueOccurrences(List<int> arr) {
6+
HashMap<int, int> map = HashMap();
7+
8+
for (int i = 0; i < arr.length; i++) {
9+
int ele = arr[i];
10+
if (map.containsKey(ele)) {
11+
map[ele] = (map[ele] ?? 0) + 1;
12+
} else {
13+
map[ele] = 1;
14+
}
15+
}
16+
17+
List<int> freq = List.filled(1000, 0); //Constraint given
18+
19+
for (MapEntry<int, int> e in map.entries) {
20+
int n = e.value;
21+
if (freq[n] != 0) {
22+
return false;
23+
}
24+
freq[n]++;
25+
}
26+
27+
return true;
28+
}
29+
30+
*/
31+
32+
func uniqueOccurrences(arr []int) bool {
33+
// hashmap to store our keys & values
34+
var hashMap = make(map[int]int)
35+
// looping through each and every element of the array
36+
for i := 0; i < len(arr); i++ {
37+
// individual element of the array
38+
var element int = arr[i]
39+
// if the hashmap have contain our key
40+
if _, key := hashMap[element]; key {
41+
// than we will assign the the value + 1
42+
hashMap[element] = hashMap[element] + 1
43+
} else {
44+
// else it's just 1
45+
hashMap[element] = 1
46+
}
47+
}
48+
// frequency array
49+
var frequency = make([]int, 1000)
50+
// _ use to skip key and than we will loop through the values of the map
51+
for _, value := range hashMap {
52+
// if the value is not zero
53+
if frequency[value] != 0 {
54+
// return false
55+
return false
56+
}
57+
// and we will walk to see next value ++ means walking
58+
frequency[value]++
59+
}
60+
// else return true
61+
return true
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# 🔥 Unique Number of Occurrences 🔥 || 3 Approaches || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1 HashMap + HashSet
4+
5+
```dart
6+
import 'dart:collection';
7+
8+
class Solution {
9+
bool uniqueOccurrences(List<int> arr) {
10+
// hashmap to store all values of the array as key value pair
11+
HashMap<int, int> map = HashMap();
12+
// for all value inside our array
13+
for (int number in arr) {
14+
// we will add value of array as key and value + 1
15+
map[number] = (map[number] ?? 0) + 1;
16+
}
17+
// hashSet to see the unique values
18+
HashSet<int> hashSet = HashSet();
19+
// we will add all the values inside the set
20+
hashSet.addAll(map.values);
21+
// if the length of the unique set is same as the length of the map than true else false
22+
return hashSet.length == map.length;
23+
}
24+
}
25+
```
26+
27+
## Solution - 2 HashSet
28+
29+
```dart
30+
import 'dart:collection';
31+
32+
class Solution {
33+
bool uniqueOccurrences(List<int> arr) {
34+
arr.sort();
35+
HashSet<int> unique = HashSet();
36+
37+
int c = 1;
38+
for (int i = 1; i < arr.length; i++) {
39+
if (arr[i] == arr[i - 1])
40+
c++;
41+
else {
42+
if (unique.contains(c)) return false;
43+
44+
unique.add(c);
45+
46+
c = 1;
47+
}
48+
}
49+
50+
if (unique.contains(c)) return false;
51+
52+
return true;
53+
}
54+
}
55+
```
56+
57+
## Solution - 3 HashMap + Frequency Array
58+
59+
```dart
60+
import 'dart:collection';
61+
62+
class Solution {
63+
bool uniqueOccurrences(List<int> arr) {
64+
HashMap<int, int> map = HashMap();
65+
66+
for (int i = 0; i < arr.length; i++) {
67+
int ele = arr[i];
68+
if (map.containsKey(ele)) {
69+
map[ele] = (map[ele] ?? 0) + 1;
70+
} else {
71+
map[ele] = 1;
72+
}
73+
}
74+
75+
List<int> freq = List.filled(1000, 0); //Constraint given
76+
77+
for (MapEntry<int, int> e in map.entries) {
78+
int n = e.value;
79+
if (freq[n] != 0) {
80+
return false;
81+
}
82+
freq[n]++;
83+
}
84+
85+
return true;
86+
}
87+
}
88+
```

0 commit comments

Comments
 (0)