Group numbers via digits frequency


  • 0
    Y

    A list of numbers are given. We need to find the total number of groups in which the digits of each number have same frequency.
    For example if numbers are:
    1
    10
    303
    330

    There are 3 groups:
    G1={1}has one 1.
    G2={10} has one 1 and one 0.
    G3={330, 303} has one 0 and two 3s.


  • 0

    @yrxwin said in Group numbers via digits frequency:

    G2={3} has one 3.

    I didn't see 3 in the list of numbers?


  • 0
    Y

    @1337c0d3r Edited. Thanks!


  • 0

    A variation of Group Anagrams?

    Just sort the digits of each number and de-dupe them.


  • 1
    T
                int re=0;
                int[] nums = { 0,10,303,330};
                HashSet<string> set = new HashSet<string>();
                foreach (int var in nums)
                {
                    string s = var.ToString();
                    char[] c = s.ToArray();
                    Array.Sort(c);
                    string cur=new string(c);
                    if (!set.Contains(cur))
                    {
                        re++;
                        set.Add(cur);
                    }
    
                }
                Console.Write(re);
    

  • 0
    D
            static void Main(string[] args)
            {
                var input = new[] {1, 10, 303, 330};
                var groups = new Dictionary<string, string>();
    
                foreach (var i in input)
                {
                    var key = string.Join("", i.ToString().OrderBy(c => c));
                    if (!groups.ContainsKey(key))
                    {
                        groups[key] = i.ToString();
                    }
                    else
                    {
                        groups[key] += $",{i}";
                    }
                }
    
                foreach (var g in groups)
                {
                    Console.WriteLine($"{g.Key} :: {g.Value}");
                }
            }
    

  • 0
    N

    Does 123 also fit into Group3?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.