Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int FindLHS2(int[] nums)
- {
- int length = 0;
- // Getting all numbers in the array without diplicates
- HashSet<int> uniqueNumbers = new HashSet<int>(nums);
- // Stores the count of a number
- Dictionary<int, int> countNumber = new Dictionary<int, int>();
- // Counting the number of repetitions
- foreach (var number in uniqueNumbers)
- {
- countNumber[number] = nums.Count(_num => _num == number);
- }
- // Finding the longest combination
- int _length;
- foreach (KeyValuePair<int, int> repeats in countNumber)
- {
- _length = repeats.Value;
- _length += countNumber.GetValueOrDefault(repeats.Key + 1, 0);
- length = Math.Max(length, _length);
- }
- return length;
- }
Advertisement
Add Comment
Please, Sign In to add comment