Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save coderRohan123/974ab4d92cfec8be18e759c3abb79b5c to your computer and use it in GitHub Desktop.
Save coderRohan123/974ab4d92cfec8be18e759c3abb79b5c to your computer and use it in GitHub Desktop.
The code snippet takes a list of integers as input and groups them into sublists based on their frequency. It returns a list of sublists where each sublist contains integers with the same frequency.

Matrix with Frequency of Numbers in a List

Preview:
from typing import List


class Solution:
    def findMatrix(self, nums: List[int]) -> List[List[int]]:
        freq = [0] * (len(nums) + 1)
        ans = []

        for c in nums:
            if freq[c] >= len(ans):
                ans.append([])

            ans[freq[c]].append(c)
            freq[c] += 1

        return ans


# Custom input
nums = [1,2,3,4]
solution = Solution()
result = solution.findMatrix(nums)
print(result)
Associated Context
Type Code Snippet ( .py )
Associated Tags Solution class findMatrix method nums parameter List[int] type hint freq variable ans list for loop if statement append operation return statement numpy ans variable append method
💡 Smart Description This code snippet finds the matrix in a given list of integers by using an array to keep track of each element and then iterating through all possible numbers.
The code snippet takes a list of integers as input and groups them into sublists based on their frequency. It returns a list of sublists where each sublist contains integers with the same frequency.
🔎 Suggested Searches Python function to find matrix in a list
How to calculate the sum of elements in an array using Python
Code snippet to find all occurrences of each element in one sorted list with zero-length values
Python code for finding matrices that are not greater than or equal to 1 and increment its length
Algorithm to determine the number of elements which overlap between 0 and len(nums)
Python code for grouping numbers in a list based on frequency
Python code for creating a matrix from a list of numbers
Python code for counting the frequency of numbers in a list
Python code for sorting numbers in a list based on frequency
Python code for creating a nested list based on the frequency of numbers in a list
Related Links https://www.w3schools.com/python/
https://www.w3schools.com/python/default.asp
https://www.w3schools.com/python/python_strings.asp
https://www.pythoncentral.io/how-to-sort-a-list-tuple-or-object-with-sorted-in-python/
https://www.pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/
https://realpython.com/iterate-through-dictionary-python/
https://www.w3resource.com/python-exercises/list/python-data-type-list-exercise-38.php
https://www.geeksforgeeks.org/sort-elements-by-frequency/
Related People Rohan Mallick
Sensitive Information No Sensitive Information Detected
Shareable Link https://ca772742-b4e2-4612-af82-29a364c1f0bb.pieces.cloud/?p=a1344eaf0c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment