isAnagram<T> function

bool isAnagram<T>(
  1. List<T> seq1,
  2. List<T> seq2
)

Checks if two sequences are anagrams of each other.

The function works with any list of comparable elements T.

It ignores case and spaces if T is String; otherwise, it compares directly.

Example:

bool result = isAnagram<String>("Listen", "Silent");
print(result); // true

Implementation

bool isAnagram<T>(List<T> seq1, List<T> seq2) {
  // If sequences differ in length, not anagrams
  if (seq1.length != seq2.length) return false;

  List<T> sorted1 = List.from(seq1);
  List<T> sorted2 = List.from(seq2);

  // If elements are String, normalize by trimming spaces and lowercasing
  if (T == String) {
    sorted1 =
        sorted1
            .cast<String>()
            .map((e) => e.replaceAll(' ', '').toLowerCase() as T)
            .toList();
    sorted2 =
        sorted2
            .cast<String>()
            .map((e) => e.replaceAll(' ', '').toLowerCase() as T)
            .toList();
  }

  sorted1.sort();
  sorted2.sort();

  return listEquals<T>(sorted1, sorted2);
}