Close

## Finding the number of duplicates in a list of numbers

Given a list of numbers, we have to find how many duplicate entries are present in it? For example consider the list of numbers {5, 1, 9, 5, 2, 1, 6, 4, 5, 8}It has 3 duplicate numbers. There are many ways to do this. In this post, I present one of the simplest ways…

## C++ STL Algorithms – Sort – Part-2

In the last post, the basic use of STL sort() method is explained. In this post, I will discuss some more options available with this method. We have seen in the previous post that sort takes the beginning and ending of the array as arguments. sort( array.begin(), array.end() ); In addition to those parameters, it…

## C++ STL Algorithms – Sort- Part-1

Sorting is one of the most widely used algorithmic primitive in programming.  C++ Standard Template Library (STL) provides an efficient implementation of the sort algorithm. It is always better to use this algorithm instead of writing our own implementation because of the following benefits. It’s performance would surely be better than your own implementation. It’s…

## Program to find the powerset of a given set

Given a set of numbers, how do we generate its power set? For example for the set {1,2} the power set is given below. {  {},  {1}, {2}, {1,2} } A power set is nothing but all the possible subsets of a given set. If a set contains N elements, it’s power set contains 2N…

## Check if an array has duplicate numbers

Given an array numbers, how do we write a function checkDuplicates() which returns true if the array has at least one element repeated; returns false if all the elements are unique. We discuss three different implementations of this function with various time and space complexities. Method-1: Naive approach This approach checks every possible pair in…

## Bracket matching problem

Stack is one of the most widely used data structure. In this post we will learn how to use the stack container (data structure) provided by C++ STL with an example. Let us consider the following problem. We have a string which contains only ‘(‘ and ‘)’ characters. We have to write a program to…

## Counting characters in the given string using C++ STL map

Map is one of the most useful data structure in solving many programming problems. Today we will see how to use C++ STL map with a simple example. Counting the frequency of letters in a given string. The map data structure stores <key,value> pairs. In this example key will be the character, and the value…

## Finding count of a number in a sorted array

In Competitive programming, learning to use the standard library than coding from scratch saves a lot of time. In this post we will use C++ STL(Standard Template Library) binary search algorithm with an example.Given a sorted array of numbers, How to find the frequency of a given number? Here is an efficient algorithm to do…