Beat 100% in C#✅ using DP
\n\n\n# Code\n\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\n\npublic class Solution {\n    public int MaxSelectedElements(int[] A) \n 
![image.png](https://assets.leetcode.com/users/images/d9c211b5-3076-4021-8939-0e58a8d124d3_1708209819.3057475.png)\n\n\n# Code\n```\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\n\npublic class Solution {\n    public int MaxSelectedElements(int[] A) \n    {\n            Dictionary<int, int> dictionary = new Dictionary<int, int>();\n            Array.Sort(A);\n            int br = 0;\n            foreach (int num in A) \n            {\n                dictionary[num + 1] = dictionary.GetValueOrDefault(num, 0) + 1;\n                dictionary[num] = dictionary.GetValueOrDefault(num - 1, 0) + 1;\n                br = Math.Max(br, Math.Max(dictionary[num], dictionary[num + 1]));\n            }\n        \n            return br;\n    }\n}\n\n```
['Dynamic Programming', 'C#']