Skip to content

Commit 26e6ac6

Browse files
committed
Adds count repeated element solution
1 parent b07d3d6 commit 26e6ac6

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ My solutions to competitive programming problems in [Geeks for Geeks](https://au
9696
|Search in sorted array|[C++](c++/search-in-sorted-array.cpp)|
9797
|Insert at end of array|[C++](c++/array-insert-at-end.cpp)|
9898
|Equilibrium point|[C++](c++/equilibrium-point.cpp)|
99+
|Left index|[C++](c++/left_index.cpp)|
100+
|Count only repeated elements|[C++](c++/count-repeated-element.cpp)|
99101
|Remove duplicate elements|[C++](c++/remove-duplicate-elements.cpp)|
100102
|Search element in array|[C++](c++/search0an0element-in-an-array.cpp)|
101103
|Check if two arrays are equals|[C++](c++/check-if-two-arrays-are-equals.cpp)|
@@ -213,6 +215,7 @@ My solutions to competitive programming problems in [Geeks for Geeks](https://au
213215
|----------|:----------------:|
214216
| Display linked list|[C++](c++/display-linked-list.cpp)|
215217
|Sum of nodes|[C++](c++/sum-of-nodes.cpp)|
218+
|Linked list insert|[C++](c++/linked-list-insert.cpp)|
216219
|Sorted insert for circular linked list|[C++](c++/sorted-insert-for-circular-linked-list.cpp)|
217220
|Join two linked list|[C++](c++/join-two-linked-list.cpp)|
218221
|Delete tail of circular linked list|[C++](c++/delete-tail-of-circular-linked-list.cpp)|

c++/count-repeated-element.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// https://practice.geeksforgeeks.org/problems/count-only-repeated/0/?track=SPCF-Searching&batchId=154
2+
/*
3+
Given an array of positive integers, where elements are consecutive (sorted).
4+
Also, there is a single element which is repeating X (any variable) number of times. Now, the task is to find the
5+
element which is repeated and number of times it is repeated.
6+
*/
7+
8+
#include <iostream>
9+
using namespace std;
10+
11+
void solve_test()
12+
{
13+
int size_list;
14+
cin >> size_list;
15+
int arr[size_list];
16+
for(int i =0; i < size_list; i++)
17+
{
18+
cin >> arr[i];
19+
}
20+
21+
int counter = 0;
22+
int element;
23+
for(int i =0; i < size_list-1; i++)
24+
{
25+
if(arr[i] == arr[i+1])
26+
element = arr[i];
27+
while(arr[i] == arr[i+1])
28+
{
29+
counter +=1;
30+
i+= 1;
31+
}
32+
}
33+
counter+=1;
34+
cout << element << " "<<counter<<endl;
35+
}
36+
37+
int main()
38+
{
39+
ios_base::sync_with_stdio(false);
40+
cin.tie(NULL);
41+
42+
long int t;
43+
cin >> t;
44+
while(t--)
45+
solve_test();
46+
return 0;
47+
}

0 commit comments

Comments
 (0)