Skip to content

Instantly share code, notes, and snippets.

@Quingsley
Last active May 12, 2024 03:53
Show Gist options
  • Save Quingsley/9be10b75e5773ee06cc8e1d8a0e39a12 to your computer and use it in GitHub Desktop.
Save Quingsley/9be10b75e5773ee06cc8e1d8a0e39a12 to your computer and use it in GitHub Desktop.
Frequecny pattern
import 'dart:math';
void main() {
final isSame = same([1, 4, 5], [16, 1, 25]);
print(isSame);
}
bool same(List<int> array1, List<int> array2) {
if (array1.length != array2.length) {
return false;
}
final frequencyCounter = <int, int>{};
final frequencyCounter1 = <int, int>{};
// Time complexity n + n + n = O(3n) = O(n)
for (final value in array1) {
frequencyCounter[value] = (frequencyCounter[value] ?? 0) + 1;
// frequencyCounter[value] == null ? 1 : (frequencyCounter[value]! + 1);
}
print(frequencyCounter);
for (final value in array2) {
frequencyCounter1[value] = (frequencyCounter1[value] ?? 0) +1;
// frequencyCounter1[value] == null ? 1 : (frequencyCounter1[value]! + 1);
}
print(frequencyCounter1);
for (final key in frequencyCounter.keys) {
print(('key - ', key));
print(('power - ', pow(key, 2)));
if (!frequencyCounter1.containsKey(pow(key, 2))) {
return false;
}
if (frequencyCounter1[pow(key, 2)] != frequencyCounter[key]) {
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment