Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Answer15.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
int majorityElement(vector<int>& nums) {
int count = 0;
int candidate = 0;

for (int num : nums) {
if (count == 0) {
candidate = num;
}
if(num==candidate) count += 1;
else count -= 1;
}

return candidate;
}
};

//Input Format: N = 3, nums[] = {3,2,3}
//Result: 3

// When we just count the occurrences of each number and compare with half of the size of the array, you will get 3 for the above solution.
10 changes: 10 additions & 0 deletions Answer55.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// It is the same structure but by using the in-built list STL data structures of C++

class Graph{
int numVertices;
list<int> *adjLists;

public:
Graph(int V);
void addEdge(int src, int dest);
};