{"id":4688,"date":"2023-10-25T12:23:55","date_gmt":"2023-10-25T06:53:55","guid":{"rendered":"https:\/\/python-programs.com\/?p=4688"},"modified":"2023-11-10T11:58:09","modified_gmt":"2023-11-10T06:28:09","slug":"python-find-duplicates-in-a-list-with-frequency-count-and-index-positions","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-find-duplicates-in-a-list-with-frequency-count-and-index-positions\/","title":{"rendered":"Python: Find Duplicates in a list with Frequency Count and index positions"},"content":{"rendered":"
A collection is an ordered list of values. There could be various types of values. A list is a mutable container. This means that existing ones can be added to, deleted from, or changed.<\/p>\n
The Python list represents the mathematical concept of a finite sequence. List values are referred to as list items or list elements. The same value may appear multiple times in a list. Each event is regarded as a distinct element.<\/p>\n
Given a list, the task is to find Duplicate elements in a list with their respective frequencies and index positions.<\/p>\n
Example:<\/strong><\/p>\n I)Without indices<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n II)With Indices<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to find duplicates in a list some of them are:<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Time Complexity: O(n^2)<\/strong><\/p>\n We’ll write a function that takes a list and prints the duplicate elements in that list, as well as their frequency counts.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Time Complexity: O(n)<\/strong><\/p>\n We can use an iterable or any dict like mapping to construct a Counter class object. This Counter object keeps track of how many times each variable in the iterable has been counted. Let’s use the Counter object to locate and count duplicates in a list.<\/p>\n This function is similar to first two points in method 2<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Time Complexity: O(n)<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Time Complexity: O(n)<\/strong><\/p>\n A collection is an ordered list of values. There could be various types of values. A list is a mutable container. This means that existing ones can be added to, deleted from, or changed. The Python list represents the mathematical concept of a finite sequence. List values are referred to as list items or list …<\/p>\ngivenlist = [1, 2, 3, 1, 2, 1, 5, 6, 7, 8, 9]<\/pre>\n
element = 1 frequency : 3\r\nelement = 2 frequency : 2<\/pre>\n
givenlist = [1, 2, 3, 1, 2, 1, 5, 6, 7, 8, 9]<\/pre>\n
element = 1 frequency : 3 indices : [0, 3, 5]\r\nelement = 2 frequency : 2 indices : [1, 4]<\/pre>\n
Find Duplicates in a list<\/h2>\n
\n
Method #1:Using set method<\/h3>\n
\n
# givenlist\r\ngivenlist = [1, 2, 3, 1, 2, 1, 5, 6, 7, 8, 9]\r\n# converting givenlist to set\r\nsetlist = set(givenlist)\r\n# Traverse the set\r\nfor element in setlist:\r\n # counting the frequency of element if it is greater than 1 then print it\r\n frequency = givenlist.count(element)\r\n if(frequency > 1):\r\n # print the element and frequency\r\n print('element =', element, 'frequency :', frequency)\r\n<\/pre>\n
element = 1 frequency : 3\r\nelement = 2 frequency : 2<\/pre>\n
Method #2:Using dictionary<\/h3>\n
\n
\nIf the element is present in dictionary keys, the value of that key is increased by one.<\/li>\n# function which print the duplicates\r\ndef printDuplicates(givenlist):\r\n # taking a empty dictionary\r\n freq = {}\r\n # Traverse the list\r\n for element in givenlist:\r\n # if the element is in freq dictionary then add its count\r\n if element in freq.keys():\r\n freq[element] += 1\r\n else:\r\n freq[element] = 1\r\n\r\n # Traverse the freq dictionary and print the duplicate elements and frequency\r\n for key in freq:\r\n # if value\/count is greater than 1 the print it\r\n if(freq[key] > 1):\r\n print('element =', key, 'frequency :', freq[key])\r\n\r\n\r\n# givenlist\r\ngivenlist = [1, 2, 3, 1, 2, 1, 5, 6, 7, 8, 9]\r\n# passing list to printDuplicates function\r\nprintDuplicates(givenlist)\r\n<\/pre>\n
element = 1 frequency : 3\r\nelement = 2 frequency : 2<\/pre>\n
Method #3:Using Counter() function<\/h3>\n
from collections import Counter\r\n# function which print the duplicates\r\n\r\n\r\ndef printDuplicates(givenlist):\r\n # using Counter function to calculate frequencies\r\n freq = Counter(givenlist)\r\n\r\n # Traverse the freq dictionary and print the duplicate elements and frequency\r\n for key in freq:\r\n # if value\/count is greater than 1 the print it\r\n if(freq[key] > 1):\r\n print('element =', key, 'frequency :', freq[key])\r\n\r\n\r\n# givenlist\r\ngivenlist = [1, 2, 3, 1, 2, 1, 5, 6, 7, 8, 9]\r\n# passing list to printDuplicates function\r\nprintDuplicates(givenlist)\r\n<\/pre>\n
element = 1 frequency : 3\r\nelement = 2 frequency : 2<\/pre>\n
Method #4:Finding indices and frequency of duplicate elements<\/h3>\n
\n
def printDuplicates(givenlist):\r\n # taking a empty dictionary\r\n freq = {}\r\n # taking index =0\r\n index = 0\r\n # Traverse the list\r\n for element in givenlist:\r\n # if the element is in freq dictionary then add its count and index\r\n if element in freq.keys():\r\n freq[element][0] += 1\r\n freq[element][1].append(index)\r\n else:\r\n freq[element] = [1, [index]]\r\n # increment the index\r\n index += 1\r\n\r\n # Traverse the freq dictionary and print the duplicate elements and frequency\r\n for key in freq:\r\n # if value\/count is greater than 1 the print it\r\n if(freq[key][0] > 1):\r\n print('element =', key, 'frequency :',\r\n freq[key][0], 'indices :', freq[key][1])\r\n\r\n\r\n# givenlist\r\ngivenlist = [1, 2, 3, 1, 2, 1, 5, 6, 7, 8, 9]\r\n# passing list to printDuplicates function\r\nprintDuplicates(givenlist)\r\n<\/pre>\n
element = 1 frequency : 3 indices : [0, 3, 5]\r\nelement = 2 frequency : 2 indices : [1, 4]<\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n