Jump to content

find 5 the most recurring items in array

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Gnomer

Gnomer

    Newbie

  • Members
  • Pip
  • 4 posts

public static List<Tuple<int, int>> GetCountOfLength(List<int> list)

        {

            List<Tuple<int, int>> res = new List<Tuple<int, int>>();

            foreach (var el in list) res.Add(Tuple.Create(list.Where(e0 => e0 == el).Count(), el));

            res.Sort();

            return res;

        }

Input param - list for examp
1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 4 5 5 5 5 5 6 6 6 6 6 6 890 890
And the result must be 1 - 13;2 -7;3 - 6;5 - 5;6 -6
But as a result I always have
1 1
1 2
1 3
and etc.
What is wrong?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
It looks like each time you add a one, you are adding a new element instead of updating it.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users