Arraylist find the count of consecutive duplicate elements
A simple approach here would be to just iterate the arraylist once, and then keep tallies as we go along:
List<Integer> list = new ArrayList<>();
list.add(20);
list.add(20);
list.add(30);
list.add(40);
list.add(40);
list.add(20);
list.add(20);
list.add(20);
Integer curr = null;
int count = 0;
System.out.print("{");
for (int val : list) {
if (curr == null) {
curr = val;
count = 1;
}
else if (curr != val) {
System.out.print("(" + curr + ", " + count + ")");
curr = val;
count = 1;
}
else {
++count;
}
}
System.out.print("(" + curr + ", " + count + ")");
System.out.print("}");
{(20, 2)(30, 1)(40, 2)(20, 3)}
Count consecutive repeated elements in array in the same sequence as they are
You can iterate over the array, counting values while they are the same, and pushing a value to the result array when they are different:
$result = [];
$count = 0;
$current = $array[0];
for ($i = 0; $i < count($array); $i++) {
if ($array[$i] == $current) {
$count++;
}
else {
$result[] = array($current, $count);
$count = 1;
}
$current = $array[$i];
}
$result[] = array($current, $count);
print_r($result);
Output:
Array
(
[0] => Array
(
[0] => a
[1] => 1
)
[1] => Array
(
[0] => b
[1] => 1
)
[2] => Array
(
[0] => a
[1] => 3
)
[3] => Array
(
[0] => c
[1] => 2
)
[4] => Array
(
[0] => b
[1] => 2
)
[5] => Array
(
[0] => a
[1] => 4
)
)
Demo on 3v4l.org
Count consecutive duplicate values in ArrayList (Kotlin)
You could manually build the list, like this:
fun count(values: List<String>): List<Group> {
val groups = mutableListOf<Group>()
values.forEach {
val last = groups.lastOrNull()
if (last?.value == it) {
last.count++
} else {
groups.add(Group(it, 1))
}
}
return groups
}
data class Group(val value: String, var count: Int)
This results in:
[Group(value=training, count=2), Group(value=assessment, count=1), Group(value=training, count=1), Group(value=assessment, count=2)]
Count of consecutive duplicate integers in a list with frequency
I didn't try running this code on an IDE but I think this should suffice:
int count = 1;
int index = 0;
byte current = bytes[index];
while (index < bytes.length - 1) {
index++;
if (bytes[index] == current) {
count++;
} else {
integers.add(current);
frequencies.add(count);
count = 1;
current = bytes[index];
}
}
integers.add(current);
frequencies.add(count);
how to count consecutive duplicates in a python list
You can use itertools.groupby
:
from itertools import groupby
list1 = [-1, -1, 1, 1, 1, -1, 1]
count_dups = [sum(1 for _ in group) for _, group in groupby(list1)]
print(count_dups)
Output:
[2, 3, 1, 1]
How to count the consecutive duplicate values in an array?
It can be done simply manually:
$arr = array(1,1,1,2,2,3,3,1,1,2,2,3);
$result = array();
$prev_value = array('value' => null, 'amount' => null);
foreach ($arr as $val) {
if ($prev_value['value'] != $val) {
unset($prev_value);
$prev_value = array('value' => $val, 'amount' => 0);
$result[] =& $prev_value;
}
$prev_value['amount']++;
}
var_dump($result);
Counting consecutive duplicates of strings from a list
Use itertools.groupby
:
from itertools import groupby
li = ['aaa','bbb','aaa','abb','abb','bbb','bbb','bbb','aaa','aaa']
a = [[i, sum(1 for i in group)] for i, group in groupby(li)]
print(a)
[['aaa', 1], ['bbb', 1], ['aaa', 1], ['abb', 2], ['bbb', 3], ['aaa', 2]]
Thank you @user3483203 for improvement:
a = [[i, len([*group])] for i, group in groupby(li)]
Count consecutive equal values in array
Here is one option adapted from this answer:
def count_consecutive(arr, n):
# pad a with False at both sides for edge cases when array starts or ends with n
d = np.diff(np.concatenate(([False], arr == n, [False])).astype(int))
# subtract indices when value changes from False to True from indices where value changes from True to False
return np.flatnonzero(d == -1) - np.flatnonzero(d == 1)
count_consecutive(a, 5)
# array([2, 1, 3])
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();
Checking for duplicate strings in JavaScript array
The findDuplicates
function (below) compares index of all items in array with index of first occurrence of same item. If indexes are not same returns it as duplicate.
let strArray = [ "q", "w", "w", "w", "e", "i", "u", "r"];
let findDuplicates = arr => arr.filter((item, index) => arr.indexOf(item) != index)
console.log(findDuplicates(strArray)) // All duplicates
console.log([...new Set(findDuplicates(strArray))]) // Unique duplicates
Related Topics
Doctrine: Update Discriminator for Single_Table Inheritance
How to Get Column Names from Pdo's Fetchall Result
What Is the ASP.NET Equivalent to PHP's Echo
Installing Mcrypt on Osx 10.8.2 - PHP 5.3.15 with Homebrew
Quoting Constants in PHP: "This Is a My_Constant"
How to Stop Curl from Using 100 Continue
How to Verify the Requesting Server in PHP
In PHP, How to Generate a Big Pseudo-Random Number
How to Update Timezonedb in PHP (Updating Timezones Info)
How to Fetch Data in PHP with MySQLi
Given an Email as Raw Text, How to Send It Using PHP
Is There a Call_User_Func() Equivalent to Create a New Class Instance
How to Convert an Integer to an Array in PHP
Using Seo User Friendly in PHP
Access an Array Returned by a Function
Redirect to 'Www' Before Ssl Requirement
If I Use Captcha Will I Be Able to Stop the Spam Completely in My Blog