Introduce difference method

Release-Notes: skip
Google-bug-id: b/236921879
Change-Id: I2bb30a5d7a948384b2f1a54adb813f1eb8b08679
diff --git a/polygerrit-ui/app/elements/change/gr-reply-dialog/gr-reply-dialog.ts b/polygerrit-ui/app/elements/change/gr-reply-dialog/gr-reply-dialog.ts
index 8efd18a..9a6e6f8 100644
--- a/polygerrit-ui/app/elements/change/gr-reply-dialog/gr-reply-dialog.ts
+++ b/polygerrit-ui/app/elements/change/gr-reply-dialog/gr-reply-dialog.ts
@@ -72,6 +72,7 @@
   areSetsEqual,
   assertIsDefined,
   containsAll,
+  difference,
   queryAndAssert,
 } from '../../../utils/common-util';
 import {CommentThread, isUnresolved} from '../../../utils/comment-util';
@@ -1292,14 +1293,10 @@
     state: ReviewerState,
     currentAccounts: AccountInput[]
   ): AccountInfo[] {
-    const existingAccounts = this.change?.reviewers[state] ?? [];
-    return existingAccounts.filter(
-      existingAccount =>
-        !currentAccounts.some(
-          currentAccount =>
-            accountOrGroupKey(currentAccount) ===
-            accountOrGroupKey(existingAccount)
-        )
+    return difference(
+      this.change?.reviewers[state] ?? [],
+      currentAccounts,
+      (a, b) => accountOrGroupKey(a) === accountOrGroupKey(b)
     );
   }
 
diff --git a/polygerrit-ui/app/utils/common-util.ts b/polygerrit-ui/app/utils/common-util.ts
index fe86d91..33ca29b 100644
--- a/polygerrit-ui/app/utils/common-util.ts
+++ b/polygerrit-ui/app/utils/common-util.ts
@@ -168,3 +168,14 @@
     result.filter(t => array.find(u => compareBy(t, u)))
   );
 }
+
+/**
+ * Returns the elements that are present in A but not present in B.
+ */
+export function difference<T>(
+  a: T[],
+  b: T[],
+  compareBy: (t: T, u: T) => boolean = (t, u) => t === u
+): T[] {
+  return a.filter(aVal => !b.some(bVal => compareBy(aVal, bVal)));
+}
diff --git a/polygerrit-ui/app/utils/common-util_test.ts b/polygerrit-ui/app/utils/common-util_test.ts
index e07401b..c9ba060 100644
--- a/polygerrit-ui/app/utils/common-util_test.ts
+++ b/polygerrit-ui/app/utils/common-util_test.ts
@@ -9,6 +9,7 @@
   areSetsEqual,
   containsAll,
   intersection,
+  difference,
 } from './common-util';
 
 suite('common-util tests', () => {
@@ -88,4 +89,11 @@
       [foo1]
     );
   });
+
+  test('difference', () => {
+    assert.deepEqual(difference([1, 2, 3], []), [1, 2, 3]);
+    assert.deepEqual(difference([1, 2, 3], [2, 3, 4]), [1]);
+    assert.deepEqual(difference([1, 2, 3], [1, 2, 3]), []);
+    assert.deepEqual(difference([1, 2, 3], [4, 5, 6]), [1, 2, 3]);
+  });
 });