- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion

# Heterogeneous Arrays in Data Sturcture

As we know the arrays are homogeneous by definition. So we have to put data of same type in an array. But if we want to store data of different type, then what will be the trick? In C like old languages, we can use unions to artificially coalesce the different types into one type. Then we can define an array on this new type. Here the kind of object that an array element actually contains is determined by a tag. Let us see one structure like this −

struct Vehicle{ int id; union { Bus b; Bike c; Car d; } };

Then the programmer would have to establish a convention on how the id tag will be used. Suppose, for example, when the id is 0, it means that the Vehicle which is represented is actually a Bus etc. The union allocates memory for the largest type among Bus, Bike and Car. This is wasteful for memory if there is a great disparity among the sizes of the objects.

In the object oriented languages, we can use the concept of inheritances. Suppose we have a class called Vehicle, and all other types like Bus, Bike and Car are the sub-class of it. So if we define an array for Vehicle, then that can also hold all of its sub-classes. This will help us to store multiple types of data in a single array.

- Related Articles
- Insertion and Deletion in Heaps in Data Sturcture
- Arrays Data Structure in Javascript
- Irregular Arrays in Data Structure
- Array of Arrays Representation in Data Structure
- Diversifying Selection in Heterogeneous Environments
- What are heterogeneous mixtures?
- Accessing Data Along Multiple Dimensions Arrays in Python Numpy
- Find Min-Max in heterogeneous list in Python
- What are the different data types of arrays in C#?
- Can a Vector contain heterogeneous objects in Java?
- Oil and Water form a heterogeneous mixture. Why?
- What is a heterogeneous mixture and homogeneous mixture?
- State whether colloidal solutions are homogeneous or heterogeneous.
- Differentiate between homogenous and heterogeneous mixtures with examples.
- Concatenate two arrays of objects and remove repeated data from an attribute in JavaScript?