{"id":12922,"date":"2021-09-30T16:30:36","date_gmt":"2021-09-30T11:00:36","guid":{"rendered":"https:\/\/python-programs.com\/?p=12922"},"modified":"2021-11-22T18:33:35","modified_gmt":"2021-11-22T13:03:35","slug":"python-program-to-find-a-number-repeating-and-missing-in-an-array-or-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-a-number-repeating-and-missing-in-an-array-or-list\/","title":{"rendered":"Python Program to Find a Number Repeating and Missing in an Array or List"},"content":{"rendered":"
Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs<\/a> examples and excel in coding the complex logic.<\/p>\n The task is to discover the missing and recurring numbers in the given n-dimensional array or list. The array’s values are in the inclusive range from 1 to n.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find a number repeating and missing in the given list.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic. The task is to discover the missing and recurring numbers in the given n-dimensional array or list. The array’s values are in the inclusive range from …<\/p>\nGiven list = [2, 6, 4, 5, 4, 3]<\/pre>\n
The element which is repeating in the given list [2, 6, 4, 5, 4, 3] is [ 4 ]\r\nThe missing element in the given list [2, 6, 4, 5, 4, 3] is [ 1 ]<\/pre>\n
Given list =[7, 1, 3, 7, 2, 5, 8, 6]<\/pre>\n
The element which is repeating in the given list [7, 1, 3, 7, 2, 5, 8, 6] is [ 7 ]\r\nThe missing element in the given list [7, 1, 3, 7, 2, 5, 8, 6] is [ 4 ]<\/pre>\n
Program to Find a Number Repeating and Missing in an Array or List in Python<\/h2>\n
\n
Method #1: Using Counter() (Hashing , Static Input)<\/h3>\n
\n
# Import the Counter() function from collections using the import keyword.\r\nfrom collections import Counter\r\n# Give the list as static input and store it in a variable.\r\ngvnlst = [2, 6, 4, 5, 4, 3]\r\n# Calculate the frequency of all the given list\r\n# elements using the Counter() function which returns\r\n# the element and its frequency as key-value pair and\r\n# store this dictionary in a variable(say freqncyValues)\r\nfreqncyValues = Counter(gvnlst)\r\n# Traverse in the freqncyValues\u00a0using For loop.\r\nfor key in freqncyValues:\r\n # Check if the value(frequency) is greater\r\n # than 1 or not using the If statement.\r\n if(freqncyValues[key] > 1):\r\n # If it is true then print it which is the\r\n # only repeating element in the given list.\r\n print('The element which is repeating in the given list',\r\n gvnlst, 'is [', key, ']')\r\n# Calculate the sum of all keys in freqncyValues\u00a0\r\n# using the sum() and keys() function and store it in a variable sumele.\r\nsumele = sum(freqncyValues.keys())\r\n# Calculate the total number of unique elements\r\n# in the given list by calculating the length of the freqncyValues\u00a0say unqelemts.\r\nunqelemts = len(freqncyValues)\r\n# Increase the value of unqelemts by 1.\r\nunqelemts = unqelemts+1\r\n# Apply the sum of n elements formula using\u00a0 (n*(n+1))\/2 where n is unqelemts.\r\nresult = ((unqelemts*(unqelemts+1))\/\/2)\r\n# Subtract the value of sumele\u00a0from the above sum and store it in another variable.\r\nresult = result-sumele\r\n# This is the missing element in the given list and prints it.\r\nprint('The missing element in the given list', gvnlst, 'is [', result, ']')\r\n<\/pre>\n
The element which is repeating in the given list [2, 6, 4, 5, 4, 3] is [ 4 ]\r\nThe missing element in the given list [2, 6, 4, 5, 4, 3] is [ 1 ]<\/pre>\n
Method #2: Using Counter() (Hashing , User Input)<\/h3>\n
\n
# Import the Counter() function from collections using the import keyword.\r\nfrom collections import Counter\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvnlst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Calculate the frequency of all the given list\r\n# elements using the Counter() function which returns\r\n# the element and its frequency as key-value pair and\r\n# store this dictionary in a variable(say freqncyValues)\r\nfreqncyValues = Counter(gvnlst)\r\n# Traverse in the freqncyValues\u00a0using For loop.\r\nfor key in freqncyValues:\r\n # Check if the value(frequency) is greater\r\n # than 1 or not using the If statement.\r\n if(freqncyValues[key] > 1):\r\n # If it is true then print it which is the\r\n # only repeating element in the given list.\r\n print('The element which is repeating in the given list',\r\n gvnlst, 'is [', key, ']')\r\n# Calculate the sum of all keys in freqncyValues\u00a0\r\n# using the sum() and keys() function and store it in a variable sumele.\r\nsumele = sum(freqncyValues.keys())\r\n# Calculate the total number of unique elements\r\n# in the given list by calculating the length of the freqncyValues\u00a0say unqelemts.\r\nunqelemts = len(freqncyValues)\r\n# Increase the value of unqelemts by 1.\r\nunqelemts = unqelemts+1\r\n# Apply the sum of n elements formula using\u00a0 (n*(n+1))\/2 where n is unqelemts.\r\nresult = ((unqelemts*(unqelemts+1))\/\/2)\r\n# Subtract the value of sumele\u00a0from the above sum and store it in another variable.\r\nresult = result-sumele\r\n# This is the missing element in the given list and prints it.\r\nprint('The missing element in the given list', gvnlst, 'is [', result, ']')\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 7 1 3 7 2 5 8 6\r\nThe element which is repeating in the given list [7, 1, 3, 7, 2, 5, 8, 6] is [ 7 ]\r\nThe missing element in the given list [7, 1, 3, 7, 2, 5, 8, 6] is [ 4 ]<\/pre>\n
\n