Preface
The .NET ecosystem is getting better and better, and more and more beginners are getting more and more friends. To deal with the same simple problem, there will be more and more ways to solve the problem as we accumulate knowledge.
When we start learning a new language, we often solve old problems that we have solved countless times in other languages. Today we will take a look at such a simple lumping question.
topic
c# Enter ten numbers and find all the numbers that have only appeared once.
Question analysis
Let's enter 10 numbers, this is very simple, use the console program()
Then cast to int. Finally, let’s find out the element that only appears once, so we can process it during the input process or after the input is completed. There are the following solutions.
Method 1
First, we introduce the simple method of being a regular one, involvingDictionary
Dictionary usage.
The main purpose of Dictionary is to provide fast key-value-based element lookups. The structure of Dictionary is generally as follows: Dictionary<[key], [value]>
We can search and count the number of input numbers as key, and finally print out data that only appears once:
Dictionary<int, int> input= new Dictionary<int, int>(); for(int i = 0; i < 10; i++) { ($"Please enter the{i+1}Number of:"); int temp = Convert.ToInt32(()); // If there is if ((temp)) { // Record input times +1 input[temp]++; } else { // Count 1 time does not exist (temp, 1); } } ($"There have been one time:"); foreach(var one in input) { if( == 1) { (); } }
Method 2
We can useList<int>
Record the user's input and check whether it has been entered before each input. If it has been entered, save it to anotherList<int>
middle. Finally, compare twoList<int>
Draw a conclusion
// Record inputList<int> numbers = new List<int> { }; // Record duplicateList<int> notthis = new List<int> { }; for (int i = 0; i < 10; i++) { ($"Please enter the{i + 1}Number of:"); int temp = Convert.ToInt32(()); if ((temp)) { (temp); } (temp); } ($"There have been one time:"); foreach (int one in numbers) { if (!(one)) { (one); } }
The following part can be usedExcept
The difference set calculation optimization is performed as:
List<int> haveone = (notthis).ToList(); ($"There has been one time:{(",", haveone)}");
Method 3
We can also use Linq to process it, first group it, and then query data that has only appeared once.
List<int> numbers = new List<int> { }; for (int i = 0; i < 10; i++) { ($"Please enter the{i + 1}Number of:"); int temp = Convert.ToInt32(()); (temp); } var linquse = (x => x) .Where(g => () == 1) .Select(s => ); ($"There has been one time:{(",", linquse)}");
Supplement: C# finds the number with the most occurrences in the array
Counting method:
First select the first number in the array, and then count from the first number in the array. For each same as the selected number, the count tree will +1, and iterate through all the numbers. count=n (n is a natural number)
Then select the second number, and then count from the first number in the array. For each same as the selected number, the count of the tree will +1, and iterate through all the numbers. count=n (n is a natural number)
And so on. . .
Before comparing, confirm whether the selected number has been selected before participating in the comparison. The code is as follows:
public static int Search(int[] arrs) { int len = ; int max = 0; //The most occurrences int num = 0; //Current number List<int> temps = new List<int>(); //a for (int i = 0; i < len; i++) { if ((arrs[i])) continue; //Exclude numbers that have participated before int count = 0; for (int j = 0; j < len; j++) { if (arrs[i] == arrs[j]) { count++; } } if (count > max) { max = count; num = arrs[i]; } (arrs[i]); //a } return num; }
Summarize
This is the article about how to use C# to find the numbers that only appear once in an array. For more related C# to find the content of the numbers that appear once in an array, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!