Math - Symmetric Difference
CoddyのC#ジャーニー「ロジックとフロー」セクションの一部 — レッスン 65/66。
2 つの集合の対称差は、どちらかの集合に属するが、それらの交差には属さない要素(ちょうど一方の集合にのみ現れる要素)を含みます。
2 つの HashSet オブジェクトを作成します:
HashSet<int> set1 = new HashSet<int>() { 1, 2, 3, 4 };
HashSet<int> set2 = new HashSet<int>() { 3, 4, 5, 6 };結果用の新しい HashSet を作成します:
HashSet<int> symmetricDifference = new HashSet<int>(set1);対称差を計算するには SymmetricExceptWith メソッドを使用します:
symmetricDifference.SymmetricExceptWith(set2);上記のコードを実行した後、symmetricDifference セットには次のものが含まれます:
{ 1, 2, 5, 6 }要素 3 と 4 は両方のセットに含まれているため、対称差には表示されません。
チャレンジ
簡単FindSymmetricDifference という名前のメソッドを作成し、2つの引数を受け取るようにしてください:
- 整数型の
HashSet(set1) - 整数型の
HashSet(set2)
このメソッドは、2つの集合の対称差を含む新しい HashSet<int> を返す必要があります。プログラムの残りの部分(入力の読み取り、ソートの実行、結果の印刷)はすでに提供されています。
チートシート
2つの集合の対称差は、どちらかの集合にありながら両方にない要素(ちょうど一方の集合に現れる要素)を含みます。
対称差を求めるには、一方の集合から新しい HashSet を作成し SymmetricExceptWith() を使用します:
HashSet<int> set1 = new HashSet<int>() { 1, 2, 3, 4 };
HashSet<int> set2 = new HashSet<int>() { 3, 4, 5, 6 };
HashSet<int> symmetricDifference = new HashSet<int>(set1);
symmetricDifference.SymmetricExceptWith(set2);結果: { 1, 2, 5, 6 } (要素 3 と 4 は両方の集合に現れるため除外されます)
自分で試してみよう
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
public static HashSet<int> FindSymmetricDifference(HashSet<int> set1, HashSet<int> set2)
{
// ここにコードを記述してください
}
static void Main(string[] args)
{
string[] input1 = Console.ReadLine().Split(',');
string[] input2 = Console.ReadLine().Split(',');
HashSet<int> set1 = new HashSet<int>();
HashSet<int> set2 = new HashSet<int>();
foreach (string s in input1)
{
if (int.TryParse(s.Trim(), out int num))
{
set1.Add(num);
}
}
foreach (string s in input2)
{
if (int.TryParse(s.Trim(), out int num))
{
set2.Add(num);
}
}
HashSet<int> result = FindSymmetricDifference(set1, set2);
// 結果を昇順で出力する
int[] orderedResult = result.OrderBy(x => x).ToArray();
foreach (int num in orderedResult)
{
Console.WriteLine(num);
}
}
}このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。
ロジックとフローのすべてのレッスン
1Multi-dimensional Arrays
2D Arrays BasicsDeclaring and Initializing 2DAccessing 2D Array ElementsNested Loops with 2D ArraysJagged ArraysCommon Matrix OperationsRecap - Multi-dimensional4Flow Control Techniques
Early ReturnsGuard ClausesJump Statements (goto)Break and ContinueFlatten Nested Conditionals7Logical Operators Advanced
Short-Circuit EvaluationConditional Logical OperatorsOperator PrecedenceRecap - Advanced Operators2Advanced Decision Making
Multiple ConditionsComplex Boolean LogicIf vs. Switch ComparisonNested Switch StatementsRecap - Advanced Decisions5Exception Handling
Try-Catch BasicsException TypesMultiple Catch BlocksWorking with FilesFinally BlockUsing vs. Try-FinallyCustom ExceptionsRecap - Error Handling3Loop Enhancements
Loop PerformanceIterating ComplexEach Loop TypeRefactoring LoopsRecap - Optimized Loops6Null Handling
Null Reference BasicsNullable Value TypesNull Checking PatternsDefensive ProgrammingRecap - Null Safety9HashMap Part 1
What is a HashMap?Declare a HashMapCheck If Key ExistsAccessing ValuesModifying DictionariesRecap - HashMap12HashSet Part 2
Math - Union of HashSetsMath - Intersection of HashSetMath - Set DifferenceMath - Symmetric DifferenceIterating Over Sets