Thursday, July 28, 2016

Leetcode 347: Find k most frequent numbers in the array - C# solution

July 28, 2016

Find k most frequent numbers in the array
Problem:
  // nums = [5, 3, 1, 1, 1, 3, 73, 1]
  // k = 1
  // return [1]

  // k = 2
  // return [1, 3]

  // k = 3

  // return [1, 3, 5]

 C# solution using Dictionary<int,int> and language Integrate Query (LINQ):

First, go through the array once, and keep the count for every distinct value:

We can use Language Integrated Query (LINQ) to call order by and then get Top k values.

Write a solution using C# first, post code here.
1. First practice, using LINQ, OrderByDescending, Take

LINQ reference:

Actionable Item:


Write one solution using bucket sort to get k most frequent numbers in the array. 


No comments:

Post a Comment