
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Minimum Index Sum for Common Elements of Two Lists in C++
Suppose two person wants to choose different cities, they have listed out the cities in different list, we have to help the, to find common choices. So we need to find those cities, those are marked by both of them.
This operation is very similar to the set intersection property, we will take two lists as set, then perform the set intersection to get the common elements.
Example
#include <iostream> #include <vector> #include <algorithm> using namespace std; vector<string> commonInterest(string set1[], int n1, string set2[], int n2) { vector<string> v(min(n1, n2)); vector<string>::iterator it; // Sorting both the list sort(set1, set1 + n1); sort(set2, set2 + n2); it = set_intersection(set1, set1 + n1, set2, set2 + n2, v.begin()); return v; } int main() { string first[] = { "Kolkata", "Hyderabad", "Chennai", "Delhi" }; int n1 = sizeof(first) / sizeof(first[0]); string second[] = { "Mumbai", "Kolkata", "Durgapur", "Delhi" }; int n2 = sizeof(second) / sizeof(second[0]); vector<string> v = commonInterest(first, n1, second, n2); cout << "Common cities: "; for (int i = 0; i < v.size(); i++) cout << ' ' << v[i]; cout << endl; }
Output
Common cities: Delhi Kolkata
- Related Articles
- Minimum Index Sum of Two Lists in C++
- C# program to print all the common elements of two lists
- Find common elements in three linked lists in C++
- Python program to print all the common elements of two lists.
- Program to find minimum difference between two elements from two lists in Python
- Minimum ASCII Delete Sum for Two Strings in C++
- Find minimum of each index in list of lists in Python
- Find sum of two array elements index wise in Java
- Find common elements in list of lists in Python
- Find count of common nodes in two Doubly Linked Lists in C++
- Adding two Python lists elements
- Find minimum shift for longest common prefix in C++
- Common element with least index sum in JavaScript
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes in Python
- C# program to find common values from two or more Lists

Advertisements