List Array Duplicates with 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");

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();

List array duplicates with count

LINQ makes this easy:

Dictionary<string, int> counts = array.GroupBy(x => x)
.ToDictionary(g => g.Key,
g => g.Count());

How to get count of duplicate objects in Array

You can easily achieve this using reduce and map. First, count all the occurrences of all objects and then add the property dynamically.

const arr = [
{
partNum: "ACDC1007",
brandName: "Electric",
supplierName: "Electric",
},
{
partNum: "ACDC1007",
brandName: "Electric",
supplierName: "Electric",
},
{
partNum: "ACDC1007",
brandName: "Electric",
supplierName: "Electric",
},
{
partNum: "ACDC1009",
brandName: "Electric",
supplierName: "Electric",
},

{
partNum: "ACDC1000",
brandName: "Electric",
supplierName: "Electric",
},
];

const countDict = arr.reduce((acc, curr) => {
const { partNum } = curr;
if (acc[partNum]) ++acc[partNum];
else acc[partNum] = 1;
return acc;
}, {});

const result = arr.map((obj) => {
obj["count"] = countDict[obj.partNum];
return obj;
});

console.log(result);

Find, count and index the duplicate items in array

An approach

    Dim list As New List(Of String) From {"a", "e", "e", "b", "c", "c", "b", "c", "d", "e"}
Dim duplicates As IEnumerable(Of String) = list.Distinct
duplicates = From s In duplicates
Where list.FindAll(Function(l) l = s).Count > 1
Select s

For Each s As String In duplicates
Dim ct As Integer = (From l In list Where l = s Select l).Count
Debug.WriteLine(s)
Debug.WriteLine("count:{0}", ct)
Dim idx As IEnumerable(Of Integer)
idx = From n In Enumerable.Range(0, list.Count)
Where list(n) = s
Select n Take ct

Debug.WriteLine("Indexes")
For Each n As Integer In idx
Debug.Write(n.ToString)
Debug.Write(" ")
Next
Debug.WriteLine("")
Next

Output

e
count:3
Indexes
1 2 9
b
count:2
Indexes
3 6
c
count:3
Indexes
4 5 7

how do i count duplicates in an array list?

As long as it works and produces the expected result, then it's fine. Plus, the solution you describe in your comment can work.
That is, create an array of 26 ints and increment it based on the letter.

It is possible to transform a character into an int by casting it. In order to numerate them from 0 to 25, you can do (int) (c - 'a') to get the index.
(int) ('a' - 'a') is 0, (int) ('b' - 'a') is 1 etc...



Related Topics



Leave a reply



Submit