Find the two numbers with odd occurrences in an unsorted array
Last Updated :
23 Jul, 2025
Given an unsorted array that contains even number of occurrences for all numbers except two numbers. The task is to find the two numbers which have odd occurrences in O(n) time complexity and O(1) extra space.
Examples:
Input: {12, 23, 34, 12, 12, 23, 12, 45}
Output: 34 45
Input: {4, 4, 100, 5000, 4, 4, 4, 4, 100, 100}
Output: 100 5000
Input: {10, 20}
Output: 10 20
[Naive Approach] - O(n^2) time and O(1) space
The idea is to run two nested loops. The outer loop picks an element and the inner loop counts the number of occurrences of the picked element. If the count of occurrences is odd, then append the element to result, while ensuring that one element is only pushed once into result.
C++
// C++ program to find the two numbers with
// odd occurrences in an unsorted array
#include <bits/stdc++.h>
using namespace std;
// Function to find the two elements
// with odd occurrences.
vector<int> twoOddNum(vector<int>& arr) {
int n = arr.size();
vector<int> ans = {-1, -1};
int index = 0;
// Check for each element
for (int i=0; i<n; i++) {
// Get the count of arr[i]
int cnt = 0;
for (int j=0; j<n; j++) {
if (arr[j]==arr[i]) cnt++;
}
// If cnt is odd and arr[i]
// has not been added to result yet.
if (cnt%2==1 && ans[0]!=arr[i] && ans[1]!=arr[i]) {
ans[index++] = arr[i];
}
}
// Return in decreasing order
if (ans[0]<ans[1]) swap(ans[0], ans[1]);
return ans;
}
int main() {
vector<int> arr = {12, 23, 34, 12, 12, 23, 12, 45};
vector<int> ans = twoOddNum(arr);
cout<<ans[0]<<" "<<ans[1]<<endl;
return 0;
}
Java
// Java program to find the two numbers with
// odd occurrences in an unsorted array
import java.util.*;
class GfG {
// Function to find the two elements
// with odd occurrences.
static int[] twoOddNum(int[] arr) {
int n = arr.length;
int[] ans = {-1, -1};
int index = 0;
// Check for each element
for (int i = 0; i < n; i++) {
// Get the count of arr[i]
int cnt = 0;
for (int j = 0; j < n; j++) {
if (arr[j] == arr[i]) cnt++;
}
// If cnt is odd and arr[i]
// has not been added to result yet.
if (cnt % 2 == 1 && ans[0] != arr[i] && ans[1] != arr[i]) {
ans[index++] = arr[i];
}
}
// Return in decreasing order
if (ans[0] < ans[1]) {
int temp = ans[0];
ans[0] = ans[1];
ans[1] = temp;
}
return ans;
}
public static void main(String[] args) {
int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
int[] ans = twoOddNum(arr);
System.out.println(ans[0] + " " + ans[1]);
}
}
Python
# Python program to find the two numbers with
# odd occurrences in an unsorted array
# Function to find the two elements
# with odd occurrences.
def twoOddNum(arr):
n = len(arr)
ans = [-1, -1]
index = 0
# Check for each element
for i in range(n):
# Get the count of arr[i]
cnt = 0
for j in range(n):
if arr[j] == arr[i]:
cnt += 1
# If cnt is odd and arr[i]
# has not been added to result yet.
if cnt % 2 == 1 and ans[0] != arr[i] and ans[1] != arr[i]:
ans[index] = arr[i]
index += 1
# Return in decreasing order
if ans[0] < ans[1]:
ans[0], ans[1] = ans[1], ans[0]
return ans
if __name__ == "__main__":
arr = [12, 23, 34, 12, 12, 23, 12, 45]
ans = twoOddNum(arr)
print(ans[0], ans[1])
C#
// C# program to find the two numbers with
// odd occurrences in an unsorted array
using System;
class GfG {
// Function to find the two elements
// with odd occurrences.
static int[] twoOddNum(int[] arr) {
int n = arr.Length;
int[] ans = {-1, -1};
int index = 0;
// Check for each element
for (int i = 0; i < n; i++) {
// Get the count of arr[i]
int cnt = 0;
for (int j = 0; j < n; j++) {
if (arr[j] == arr[i]) cnt++;
}
// If cnt is odd and arr[i]
// has not been added to result yet.
if (cnt % 2 == 1 && ans[0] != arr[i] && ans[1] != arr[i]) {
ans[index++] = arr[i];
}
}
// Return in decreasing order
if (ans[0] < ans[1]) {
int temp = ans[0];
ans[0] = ans[1];
ans[1] = temp;
}
return ans;
}
static void Main(string[] args) {
int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
int[] ans = twoOddNum(arr);
Console.WriteLine(ans[0] + " " + ans[1]);
}
}
JavaScript
// JavaScript program to find the two numbers with
// odd occurrences in an unsorted array
// Function to find the two elements
// with odd occurrences.
function twoOddNum(arr) {
let n = arr.length;
let ans = [-1, -1];
let index = 0;
// Check for each element
for (let i = 0; i < n; i++) {
// Get the count of arr[i]
let cnt = 0;
for (let j = 0; j < n; j++) {
if (arr[j] == arr[i]) cnt++;
}
// If cnt is odd and arr[i]
// has not been added to result yet.
if (cnt % 2 == 1 && ans[0] != arr[i] && ans[1] != arr[i]) {
ans[index++] = arr[i];
}
}
// Return in decreasing order
if (ans[0] < ans[1]) {
[ans[0], ans[1]] = [ans[1], ans[0]];
}
return ans;
}
let arr = [12, 23, 34, 12, 12, 23, 12, 45];
let ans = twoOddNum(arr);
console.log(ans[0], ans[1]);
[Better Approach - 1] Using Sorting - O(n logn) time and O(1) space
The idea is to use sorting and then find the frequency of each element in linear time.
C++
// C++ program to find the two numbers with
// odd occurrences in an unsorted array
#include <bits/stdc++.h>
using namespace std;
// Function to find the two elements
// with odd occurrences.
vector<int> twoOddNum(vector<int>& arr) {
int n = arr.size();
vector<int> ans = {-1, -1};
int index = 0;
// Sort the array
sort(arr.begin(), arr.end());
// Get count of each element
int i = 0;
while (i<n) {
int val = arr[i];
int cnt = 0;
while (i<n && arr[i]==val) {
cnt++;
i++;
}
// If count is odd
if (cnt%2==1) ans[index++] = val;
}
// Return in decreasing order
if (ans[0]<ans[1]) swap(ans[0], ans[1]);
return ans;
}
int main() {
vector<int> arr = {12, 23, 34, 12, 12, 23, 12, 45};
vector<int> ans = twoOddNum(arr);
cout<<ans[0]<<" "<<ans[1]<<endl;
return 0;
}
Java
// Java program to find the two numbers with
// odd occurrences in an unsorted array
import java.util.Arrays;
class GfG {
// Function to find the two elements
// with odd occurrences.
static int[] twoOddNum(int[] arr) {
int n = arr.length;
int[] ans = {-1, -1};
int index = 0;
// Sort the array
Arrays.sort(arr);
// Get count of each element
int i = 0;
while (i < n) {
int val = arr[i];
int cnt = 0;
while (i < n && arr[i] == val) {
cnt++;
i++;
}
// If count is odd
if (cnt % 2 == 1) ans[index++] = val;
}
// Return in decreasing order
if (ans[0] < ans[1]) {
int temp = ans[0];
ans[0] = ans[1];
ans[1] = temp;
}
return ans;
}
public static void main(String[] args) {
int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
int[] ans = twoOddNum(arr);
System.out.println(ans[0] + " " + ans[1]);
}
}
Python
# Python program to find the two numbers with
# odd occurrences in an unsorted array
# Function to find the two elements
# with odd occurrences.
def twoOddNum(arr):
n = len(arr)
ans = [-1, -1]
index = 0
# Sort the array
arr.sort()
# Get count of each element
i = 0
while i < n:
val = arr[i]
cnt = 0
while i < n and arr[i] == val:
cnt += 1
i += 1
# If count is odd
if cnt % 2 == 1:
ans[index] = val
index += 1
# Return in decreasing order
if ans[0] < ans[1]:
ans[0], ans[1] = ans[1], ans[0]
return ans
if __name__ == "__main__":
arr = [12, 23, 34, 12, 12, 23, 12, 45]
ans = twoOddNum(arr)
print(ans[0], ans[1])
C#
// C# program to find the two numbers with
// odd occurrences in an unsorted array
using System;
class GfG {
// Function to find the two elements
// with odd occurrences.
static int[] twoOddNum(int[] arr) {
int n = arr.Length;
int[] ans = {-1, -1};
int index = 0;
// Sort the array
Array.Sort(arr);
// Get count of each element
int i = 0;
while (i < n) {
int val = arr[i];
int cnt = 0;
while (i < n && arr[i] == val) {
cnt++;
i++;
}
// If count is odd
if (cnt % 2 == 1) ans[index++] = val;
}
// Return in decreasing order
if (ans[0] < ans[1]) {
int temp = ans[0];
ans[0] = ans[1];
ans[1] = temp;
}
return ans;
}
static void Main(string[] args) {
int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
int[] ans = twoOddNum(arr);
Console.WriteLine(ans[0] + " " + ans[1]);
}
}
JavaScript
// JavaScript program to find the two numbers with
// odd occurrences in an unsorted array
// Function to find the two elements
// with odd occurrences.
function twoOddNum(arr) {
let n = arr.length;
let ans = [-1, -1];
let index = 0;
// Sort the array
arr.sort((a, b) => a - b);
// Get count of each element
let i = 0;
while (i < n) {
let val = arr[i];
let cnt = 0;
while (i < n && arr[i] === val) {
cnt++;
i++;
}
// If count is odd
if (cnt % 2 === 1) {
ans[index++] = val;
}
}
// Return in decreasing order
if (ans[0] < ans[1]) {
[ans[0], ans[1]] = [ans[1], ans[0]];
}
return ans;
}
let arr = [12, 23, 34, 12, 12, 23, 12, 45];
let ans = twoOddNum(arr);
console.log(ans[0], ans[1]);
[Better Approach - 2] Using Hash Map - O(n) time and O(n) space
The idea is to use a hash map to store the count of all values. Then iterate through the map and return the values with odd count.
Step by step approach:
- Traverse all elements and insert them in to a hash table. Element is used as key and the frequency is used as the value in the hash table.
- Iterate through the map and append the values with odd count.
C++
// C++ program to find the two numbers with
// odd occurrences in an unsorted array
#include <bits/stdc++.h>
using namespace std;
// Function to find the two elements
// with odd occurrences.
vector<int> twoOddNum(vector<int>& arr) {
int n = arr.size();
vector<int> ans = {-1, -1};
int index = 0;
// Map to store count
unordered_map<int,int> cnt;
// Count occurrences in array
for (int i=0; i<n; i++) {
cnt[arr[i]]++;
}
// Append the values with odd
// count to result
for (auto p: cnt) {
if (p.second%2==1) {
ans[index++] = p.first;
}
}
// Return in decreasing order
if (ans[0]<ans[1]) swap(ans[0], ans[1]);
return ans;
}
int main() {
vector<int> arr = {12, 23, 34, 12, 12, 23, 12, 45};
vector<int> ans = twoOddNum(arr);
cout<<ans[0]<<" "<<ans[1]<<endl;
return 0;
}
Java
// Java program to find the two numbers with
// odd occurrences in an unsorted array
import java.util.HashMap;
class GfG {
// Function to find the two elements
// with odd occurrences.
static int[] twoOddNum(int[] arr) {
int n = arr.length;
int[] ans = {-1, -1};
int index = 0;
// Map to store count
HashMap<Integer, Integer> cnt = new HashMap<>();
// Count occurrences in array
for (int i = 0; i < n; i++) {
cnt.put(arr[i], cnt.getOrDefault(arr[i], 0) + 1);
}
// Append the values with odd
// count to result
for (var entry : cnt.entrySet()) {
if (entry.getValue() % 2 == 1) {
ans[index++] = entry.getKey();
}
}
// Return in decreasing order
if (ans[0] < ans[1]) {
int temp = ans[0];
ans[0] = ans[1];
ans[1] = temp;
}
return ans;
}
public static void main(String[] args) {
int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
int[] ans = twoOddNum(arr);
System.out.println(ans[0] + " " + ans[1]);
}
}
Python
# Python program to find the two numbers with
# odd occurrences in an unsorted array
# Function to find the two elements
# with odd occurrences.
def twoOddNum(arr):
n = len(arr)
ans = [-1, -1]
index = 0
# Map to store count
cnt = {}
# Count occurrences in array
for num in arr:
cnt[num] = cnt.get(num, 0) + 1
# Append the values with odd
# count to result
for key, value in cnt.items():
if value % 2 == 1:
ans[index] = key
index += 1
# Return in decreasing order
if ans[0] < ans[1]:
ans[0], ans[1] = ans[1], ans[0]
return ans
if __name__ == "__main__":
arr = [12, 23, 34, 12, 12, 23, 12, 45]
ans = twoOddNum(arr)
print(ans[0], ans[1])
C#
// C# program to find the two numbers with
// odd occurrences in an unsorted array
using System;
using System.Collections.Generic;
class GfG {
// Function to find the two elements
// with odd occurrences.
static int[] twoOddNum(int[] arr) {
int n = arr.Length;
int[] ans = {-1, -1};
int index = 0;
// Map to store count
Dictionary<int, int> cnt = new Dictionary<int, int>();
// Count occurrences in array
for (int i = 0; i < n; i++) {
if (cnt.ContainsKey(arr[i])) {
cnt[arr[i]]++;
} else {
cnt[arr[i]] = 1;
}
}
// Append the values with odd
// count to result
foreach (var entry in cnt) {
if (entry.Value % 2 == 1) {
ans[index++] = entry.Key;
}
}
// Return in decreasing order
if (ans[0] < ans[1]) {
int temp = ans[0];
ans[0] = ans[1];
ans[1] = temp;
}
return ans;
}
static void Main(string[] args) {
int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
int[] ans = twoOddNum(arr);
Console.WriteLine(ans[0] + " " + ans[1]);
}
}
JavaScript
// JavaScript program to find the two numbers with
// odd occurrences in an unsorted array
// Function to find the two elements
// with odd occurrences.
function twoOddNum(arr) {
let n = arr.length;
let ans = [-1, -1];
let index = 0;
// Map to store count
let cnt = new Map();
// Count occurrences in array
for (let i = 0; i < n; i++) {
cnt.set(arr[i], (cnt.get(arr[i]) || 0) + 1);
}
// Append the values with odd
// count to result
for (let [key, value] of cnt) {
if (value % 2 === 1) {
ans[index++] = key;
}
}
// Return in decreasing order
if (ans[0] < ans[1]) {
[ans[0], ans[1]] = [ans[1], ans[0]];
}
return ans;
}
let arr = [12, 23, 34, 12, 12, 23, 12, 45];
let ans = twoOddNum(arr);
console.log(ans[0], ans[1]);
[Expected Approach] Using Bit Manipulation - O(n) time and O(1) space
The idea is to first compute the XOR of all array elements, which results in xorVal = x^y (where x and y are our target numbers) since even count elements cancel each other out (as a^a = 0).
Now we have value of x^y, but we need to find the values of individual elements x and y. To find these values, we divide array elements into two sets.
We find the rightmost set bit in xorVal, which indicates a position where x and y differ (one has 1 and other has 0). Using this bit position as a filter, we partition numbers into two sets - one set where this bit is set and another where it's not. This separation ensures that x and y go into different sets.
Finally, we XOR all numbers in each set separately; even count numbers cancel out again, leaving us with one unique number in each set, which we return in decreasing order.
Step by step approach:
- The first step is to do XOR of all elements present in array (lets say xorVal). XOR of all elements gives us XOR of x and y because of the following properties of XOR operation:
- XOR of any number n with itself gives us 0, i.e., n ^ n = 0
- XOR of any number n with 0 gives us n, i.e., n ^ 0 = n
- XOR is cumulative and associative.
- Pick a set bit in xorVal, which indicates a position where x and y differ (one has 1 and other has 0). So we separate x and y into two different groups, along with rest of the numbers of list, based on whether the number has same set-bit or not.
- We will choose right most bit in this case. We can get right most set bit by performing AND operation of xorVal with its negative counterpart (xorVal & -xorVal).
- In third step, we separate x and y into two different groups. We now know that for selected set bit index, x and y have different corresponding bits. If we AND all numbers in list with set bit, some will give 0 and others will give 1. We will put all numbers giving zeroes in one group and ones in another.
- Return the result of the two groups in decreasing order.
Illustration:
Taking example of arr = [4, 2, 4, 10, 2, 3, 3, 12]
- XOR of all values in the array will be equal to
- 4 ^ 2 ^ 4 ^ 10 ^ 2 ^ 3 ^ 3 ^ 12
- (4 ^ 4) ^ (2 ^ 2) ^ 10 ^ (3 ^ 3) ^ 12
- 0 ^ 0 ^ 10 ^ 0 ^ 12
- 10 ^ 12
- 6
- Find right most set bit of xorVal:
- 6 & (-6)
- 0110 & 1010 (Binary representation)
- 0010
- 2
- Separate all values on basis of set bit and find their xor value:
- Values with set bit
- Values with unset bit
- Return the result in decreasing order, i.e., {12, 10}.
Below is the implementation of the above approach:
C++
// C++ program to find the two numbers with
// odd occurrences in an unsorted array
#include <bits/stdc++.h>
using namespace std;
// Function to find the two elements
// with odd occurrences.
vector<int> twoOddNum(vector<int>& arr) {
int n = arr.size();
// Get the XOR of the two numbers we need to find
int xorVal = 0;
for (int i=0; i<n; i++) {
xorVal = arr[i] ^ xorVal;
}
// Get its last set bit
xorVal &= -xorVal;
vector<int> ans(2, 0);
for (int i=0; i<n; i++) {
int num = arr[i];
// If bit is not set, it
// belongs to first set.
if ((num & xorVal) == 0) {
ans[0] ^= num;
}
// If bit is set, it
// belongs to 2nd set.
else {
ans[1] ^= num;
}
}
// Return in decreasing order
if (ans[0]<ans[1]) swap(ans[0], ans[1]);
return ans;
}
int main() {
vector<int> arr = {12, 23, 34, 12, 12, 23, 12, 45};
vector<int> ans = twoOddNum(arr);
cout<<ans[0]<<" "<<ans[1]<<endl;
return 0;
}
Java
// Java program to find the two numbers with
// odd occurrences in an unsorted array
import java.util.*;
class GfG {
// Function to find the two elements
// with odd occurrences.
static int[] twoOddNum(int[] arr) {
int n = arr.length;
// Get the XOR of the two numbers we need to find
int xorVal = 0;
for (int i = 0; i < n; i++) {
xorVal = arr[i] ^ xorVal;
}
// Get its last set bit
xorVal &= -xorVal;
int[] ans = {0, 0};
for (int i = 0; i < n; i++) {
int num = arr[i];
// If bit is not set, it
// belongs to first set.
if ((num & xorVal) == 0) {
ans[0] ^= num;
}
// If bit is set, it
// belongs to 2nd set.
else {
ans[1] ^= num;
}
}
// Return in decreasing order
if (ans[0] < ans[1]) {
int temp = ans[0];
ans[0] = ans[1];
ans[1] = temp;
}
return ans;
}
public static void main(String[] args) {
int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
int[] ans = twoOddNum(arr);
System.out.println(ans[0] + " " + ans[1]);
}
}
Python
# Python program to find the two numbers with
# odd occurrences in an unsorted array
# Function to find the two elements
# with odd occurrences.
def twoOddNum(arr):
n = len(arr)
# Get the XOR of the two numbers we need to find
xorVal = 0
for num in arr:
xorVal ^= num
# Get its last set bit
xorVal &= -xorVal
ans = [0, 0]
for num in arr:
# If bit is not set, it
# belongs to first set.
if (num & xorVal) == 0:
ans[0] ^= num
# If bit is set, it
# belongs to 2nd set.
else:
ans[1] ^= num
# Return in decreasing order
if ans[0] < ans[1]:
ans[0], ans[1] = ans[1], ans[0]
return ans
if __name__ == "__main__":
arr = [12, 23, 34, 12, 12, 23, 12, 45]
ans = twoOddNum(arr)
print(ans[0], ans[1])
C#
// C# program to find the two numbers with
// odd occurrences in an unsorted array
using System;
class GfG {
// Function to find the two elements
// with odd occurrences.
static int[] twoOddNum(int[] arr) {
int n = arr.Length;
// Get the XOR of the two numbers we need to find
int xorVal = 0;
for (int i = 0; i < n; i++) {
xorVal = arr[i] ^ xorVal;
}
// Get its last set bit
xorVal &= -xorVal;
int[] ans = {0, 0};
for (int i = 0; i < n; i++) {
int num = arr[i];
// If bit is not set, it
// belongs to first set.
if ((num & xorVal) == 0) {
ans[0] ^= num;
}
// If bit is set, it
// belongs to 2nd set.
else {
ans[1] ^= num;
}
}
// Return in decreasing order
if (ans[0] < ans[1]) {
int temp = ans[0];
ans[0] = ans[1];
ans[1] = temp;
}
return ans;
}
static void Main(string[] args) {
int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
int[] ans = twoOddNum(arr);
Console.WriteLine(ans[0] + " " + ans[1]);
}
}
JavaScript
// JavaScript program to find the two numbers with
// odd occurrences in an unsorted array
// Function to find the two elements
// with odd occurrences.
function twoOddNum(arr) {
let n = arr.length;
// Get the XOR of the two numbers we need to find
let xorVal = 0;
for (let i = 0; i < n; i++) {
xorVal = arr[i] ^ xorVal;
}
// Get its last set bit
xorVal &= -xorVal;
let ans = [0, 0];
for (let i = 0; i < n; i++) {
let num = arr[i];
// If bit is not set, it
// belongs to first set.
if ((num & xorVal) === 0) {
ans[0] ^= num;
}
// If bit is set, it
// belongs to 2nd set.
else {
ans[1] ^= num;
}
}
// Return in decreasing order
if (ans[0] < ans[1]) {
[ans[0], ans[1]] = [ans[1], ans[0]];
}
return ans;
}
let arr = [12, 23, 34, 12, 12, 23, 12, 45];
let ans = twoOddNum(arr);
console.log(ans[0], ans[1]);
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem