Python: count repeated elements in the list
You can do that using count
:
my_dict = {i:MyList.count(i) for i in MyList}
>>> print my_dict #or print(my_dict) in python-3.x
{'a': 3, 'c': 3, 'b': 1}
Or using collections.Counter
:
from collections import Counter
a = dict(Counter(MyList))
>>> print a #or print(a) in python-3.x
{'a': 3, 'c': 3, 'b': 1}
Trying to get a count of duplicate elements
If the Count is greater than one, check if a
is not in the dup
list, then add a
to it.
Finally, print the length of the dup
list
n=int(input("Enter the number of products to be stored in a list : "))
list1=[]
for i in range(n):
item = int(input("value for product " + str(i+1) + " : "))
list1.append(item)
dup = []
for a in list1:
if (list1.count(a) > 1) and (a not in dup):
dup.append(a)
print("Count of duplicate elements in the list: ", len(dup))
How do you find most duplicates in a 2d list?
One line splitted here:
[ a[k]
for k in range(len(a))
if a.count( a[k] ) > 1
and k == a.index( a[k] ) ]
Count of duplicate items in a C# list
If you just need the total count:
var total = colorList.GroupBy(_ => _).Where(_ => _.Count() > 1).Sum(_ => _.Count());
An alternative which might be faster with large data sets:
var hashset = new HashSet<string>(); // to determine if we already have seen this color
var duplicates = new HashSet<string>(); // will contain the colors that are duplicates
var count = 0;
foreach (var color in colorList)
{
if (!hashset.Add(color))
{
count++;
if (duplicates.Add(color))
count++;
}
}
UPDATE: measured both methods with a list of 2^25 (approx. 30 million) entries: first one 3.7 seconds, second one 3.2 seconds.
How to count duplicate value in an array in javascript
function count() { array_elements = ["a", "b", "c", "d", "e", "a", "b", "c", "f", "g", "h", "h", "h", "e", "a"];
array_elements.sort();
var current = null; var cnt = 0; for (var i = 0; i < array_elements.length; i++) { if (array_elements[i] != current) { if (cnt > 0) { document.write(current + ' comes --> ' + cnt + ' times<br>'); } current = array_elements[i]; cnt = 1; } else { cnt++; } } if (cnt > 0) { document.write(current + ' comes --> ' + cnt + ' times'); }
}
count();
How to count duplicate elements in ArrayList?
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("a");
list.add("a");
list.add("a");
int countA=Collections.frequency(list, "a");
int countB=Collections.frequency(list, "b");
int countC=Collections.frequency(list, "c");
Related Topics
Find Largest Number from User Input
Converting Json into Newline Delimited Json in Python
How to Convert .Dat to .Csv Using Python
How to Change Border Color in Tkinter Widget
Python: Import Cx_Oracle Importerror: No Module Named Cx_Oracle Error Is Thown
Python, Deleting All Files in a Folder Older Than X Days
Visual Studio Code Windows , Python Pandas . No Module Named Pandas
Send a File Through Sockets in Python
Pandas Map One Column to the Combination of Two Columns
How to Solve and Equation With Inputs in Python
Pandas - How to Compare 2 CSV Files and Output Changes
How to Pad a String With Leading Zeros in Python 3
Pandas - Drop Last Column of Dataframe
Python Comparing List Values to Keys in List of Dicts
Could Not Find a Version That Satisfies the Requirement in Python