How to Insert Element(s) at a Specified Index in an Array

Other Blogs

Blogs ❯❯ DSA

Image could not load

Insert Elements In Array by Markus Spiske on Unsplash

जब हम किसी programming languages में arrays के साथ काम करते हैं, तो कभी कभी हमें existing array में किसी particular index पर element insert करना पड़ता है।

हम ये कैसे कर सकते हैं manually? मतलब बिना किसी predefined method का use किये? आज हम देखेंगे कि कैसे हम same array को modify करते हुए, एक या ज़्यादा elements को specified index पर insert कर सकते हैं।

इस blog में हम दोनो तरह से examples देखेंगे:

  1. Single Element Insert - एक ही element को किसी specified index पर insert करना.

  2. Multiple Elements Insert - एक या ज़्यादा elements को एक साथ किसी specified index पर insert करना.

सब कुछ manually किया जायेगा, बिना किसी predefined method का use किये।

Insert an element on a specified index in array

सबसे पहले, हम single element को एक specified index पर insert करेंगे।

<?php // Function to insert a single element at a specified index function insertElementAtIndex($array, $index, $element) { // Loop through the array from the end to the index for ($i = count($array); $i > $index; $i--) { $array[$i] = $array[$i - 1]; // Shift elements to the right } // Insert the new element at the specified index $array[$index] = $element; return $array; } ?>

Explanation:

  • insertElementAtIndex() function को हम array, index, और element pass करते हैं।

  • हम array को reverse direction में loop करते हैं (from end तो थे index) और elements को एक position shift करते हैं right . 

  • फिर हम specified index पर new element insert कर देते हैं।

Insert Multiple Elements In an Array at a Specific Index

अब हम multiple elements को एक साथ specified index पर insert करेंगे। ये थोड़ा complex हो सकता है, क्योंकि हमें elements को shift करने के बाद, एक साथ multiple elements को insert करना पड़ेगा।

<?php // Function to insert multiple elements at a specified index function insertMultipleElementsAtIndex($array, $index, $elements) { // Loop through the array from the end to the index $numElements = count($elements); $arrayLength = count($array); // Shift elements to the right to make space for the new elements for ($i = $arrayLength - 1; $i >= $index; $i--) { $array[$i + $numElements] = $array[$i]; } // Insert new elements at the specified index for ($i = 0; $i < $numElements; $i++) { $array[$index + $i] = $elements[$i]; } return $array; } ?>
Explanation 

अगर आप थोड़ा ध्यान देंगे तो एक pattern दिखेगा दोनों examples में !

हमें element insert करने के लिए right side में space बनाया है जितना भी elements insert करने। जब सिर्फ 1 element add करना था तो तो सिर्फ 1 index ही shift की थी।

और जब multiple elements insert करने थे तो $array[$i + $numElements] = $array[$i]; use किया। जहाँ $i last index है और $numElements insert किये जा रहे numbers का count है।

तो अगर हमें 3 elements एक साथ insert करने हुए तो 3 space right side से बनेंगे।

Key Points To Remember

  • Single Element Insertion : जब आप एक ही element को insert कर रहे हैं, तो आपको array को reverse direction में loop करना पड़ता है और elements को shift करते हुए desired index पर insert करते हैं।

  • Multiple Elements Insertion: Multiple elements को insert करते वक्त, पहले आपको उन elements के लिए space बनानी पड़ती है (elements को shift करना पड़ता है). फिर उनको sequentially insert किया जाता है। 

  • Array Modification : इन दोनो cases में, हम directly same array को modify करते हैं, नया array create नहीं करते।

Conclusion

आज हमने manually , single और multiple elements को specified index पर insert करने का process देखा। हमने array shifting और manual element insertion का use किया, बिना किसी predefined functions का use किये।

यह approach useful है जब आपको custom array manipulation करनी हो और new array create करने का time नहीं हो।

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 5.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook

Your Thought ?

Please wait . . .

    Recent Blogs

    Loading ...

    0 Comment(s) found !