Skip to content

[BUG] - <title>Incorrect Expected Output for Test Case #31685

@vinodmalla

Description

@vinodmalla

LeetCode Username

mallavinod95

Problem Number, Title, and Link

https://leetcode.com/problems/top-k-frequent-elements/description/

Bug Category

Incorrect test case (Output of test case is incorrect as per the problem statement)

Bug Description

The problem states:
Problem Number 347
Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.

For the following test case:

Input: nums = [1,2,1,2,1,2,3,1,3,2], k = 2

Frequency of elements:

1 → 4 times

2 → 4 times

3 → 2 times

The two most frequent elements are 1 and 2, so a valid output is:

[1,2]

However, the platform currently shows the expected output as:

[-1]

This contradicts the problem statement.

Language Used for Code

JavaScript

Code used for Submit/Run operation

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number[]}
 */
var topKFrequent = function(nums, k) {
    let a={}
    for(let i=0;i<nums.length;i++){
        if(!a[nums[i]]){
            a[nums[i]]=1
        }
        else{
            a[nums[i]]++
        }
    }
    let pq=new MinPriorityQueue(x=>x.freq);
    for(key in a){
        pq.push({val:key,freq:a[key]})
        if(pq.size()>k){
            pq.pop()
        }
    }
    return pq.toArray().map(x=>Number(x.val))

};

Expected behavior

For the following test case:

Input: nums = [1,2,1,2,1,2,3,1,3,2], k = 2

Frequency of elements:

1 → 4 times

2 → 4 times

3 → 2 times

The two most frequent elements are 1 and 2, so a valid output is:

[1,2]

Screenshots

No response

Additional context

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions