Find Common Elements in Two Arrays – Intersection of two Arrays

Given two sorted arrays, Write a code to find common elements in two arrays. In other words, we have to write a code to find the intersection of two arrays.

Find Common Elements in Two Arrays

Let’s assume following two sorted arrays arr1[] and arr2[].

int arr1[] = {2,  3,  4,  5,  6};
int arr2[] = {4,  6,  7,  8,  9};

So the common elements in these two arrays is 4 and 6.

Program to Search an Element in an Array

Write a program to search an element in an array. Given a sorted array, how do you search an element in an array. Think for a moment how do you solve this problem.

You can use multiple approaches to find the solution of this problem. But try to solve this problem in minimum time complexity.

Find the Second Largest Number in an Unsorted Array

Write a Program to find the second largest number in an unsorted array. Given an array of unsorted values find the second largest number in an array.

For example – Let’s take an array.

arr[] = { 4, 1, 5, 2, 8, 0, 3, 55}

The second largest element in this array is 8.  So how do you find the second largest number in an array ?  There are multiple ways by which you can solve this problem.

Find Second Largest Number in an Array

Find Second Largest Number in an Array

First Approach : Sort an Array

The simple approach is  to sort an array using sorting algorithm. After sorting an array element at n-2 index will be the second largest element.

You can use following sorting algorithms.

i) Selection Sort

ii) Insertion Sort

iii) Bubble Sort

iv) Merge Sort

v)Quick Sort

Selection sort, insertion sort and bubble sort are not suitable for large data sets as their time complexity is O(n2) . You can use Merge Sort or Quick Sort for large data sets.

NOTE –  If  values of an array is  repeated then this approach won’t work. Then the best approach is to traverse an array to find the second largest element.

Second Approach : Traverse an array

The best approach is to traverse an array to find the second largest number in an array. Using this approach maintain two flags one for highest and other for the second highest number. The time complexity of this approach is O(n).

Find Missing number in Array – Programming Problem

How to find missing number in array of 1 to n. This question is generally asked in technical interviews. The problem statement is something like that. Given an array of numbers from 1 to 100. The array is sorted. One number is missing from an array. You have to find the missing number from an array. Also there is no duplicates in an array.

Stack Program in C using an Array – Stack Tutorial Part – I

Write a stack program in C using an array. Implement a stack data structure using an Array. In this tutorial, We’ll learn about stack data structure and it’s implementation using an array.

C Program to Implement a Stack using Linked List

What is a Stack in Data Structure?

A Stack is a Data Structure, in which insertion and deletion operations are allowed only at one end. It worked on LIFO (Last In First Out) Principle. In LIFO, an element which inserted last must be the first element to be removed.