Feedback Form

Core Operations: add(), get(), remove(), contains() – Time Complexity Analysis

Core Operations in ArrayList: add(), get(), remove(), contains() – Time Complexity Analysis

जब हम Java में ArrayList use करते हैं, तो सबसे ज़रूरी बात होती है इसके core operations को समझना — जैसे add(), get(), remove() और contains()। ये वो basic functions हैं जो किसी भी student के लिए exam या interview में सबसे ज़्यादा पूछे जाते हैं। इस blog में हम इन्हीं operations को detail में, simple भाषा में और time complexity के साथ समझेंगे।

1. add() Method in ArrayList

add() method का use किसी existing ArrayList में नया element जोड़ने के लिए किया जाता है। ये दो forms में available होता है:

  • add(E element): यह method element को list के end में add करता है।
  • add(int index, E element): यह element को किसी specific index पर insert करता है।

How add() Works Internally

जब भी आप add() method call करते हैं, Java internally check करता है कि ArrayList की current capacity पूरी हुई या नहीं। अगर capacity भर गई है, तो यह automatically new array बनाता है (usually old capacity × 1.5) और सारे elements को उसमें copy करता है। उसके बाद नया element add होता है।

Example:

ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Mango");
list.add("Banana");
System.out.println(list);

Time Complexity of add()

Operation Type Time Complexity Explanation
add(E element) O(1) (Amortized) Normally element end में add होता है, इसलिए O(1)। लेकिन कभी-कभी resizing होने पर O(n) भी लग सकता है।
add(int index, E element) O(n) क्योंकि specific index पर element डालने के लिए बाकी elements को shift करना पड़ता है।

2. get() Method in ArrayList

get() method किसी particular index से element निकालने के लिए use होता है। ये सबसे fast operations में से एक है क्योंकि ArrayList internally array-based structure होती है।

Syntax:

E element = list.get(index);

Example:

ArrayList<Integer> nums = new ArrayList<>();
nums.add(10);
nums.add(20);
nums.add(30);
System.out.println(nums.get(1)); // Output: 20

Time Complexity of get()

Operation Time Complexity Explanation
get(index) O(1) क्योंकि ArrayList direct indexing (random access) use करती है, इसलिए किसी भी index पर पहुँचना instant होता है।

3. remove() Method in ArrayList

remove() method का काम किसी element को list से हटाना होता है। यह दो forms में available है:

  • remove(int index): किसी specific index पर मौजूद element को हटाता है।
  • remove(Object o): किसी specific value को remove करता है।

Example:

ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Mango");
fruits.add("Banana");
fruits.remove("Mango");
System.out.println(fruits); // Output: [Apple, Banana]

How remove() Works Internally

जब किसी element को remove किया जाता है, तो ArrayList उस element के बाद आने वाले सारे elements को एक step पीछे shift कर देता है ताकि gaps न रहें। इसी shifting process की वजह से remove operation थोड़ा slow होता है।

Time Complexity of remove()

Operation Time Complexity Explanation
remove(int index) O(n) क्योंकि index के बाद वाले सभी elements को shift करना पड़ता है।
remove(Object o) O(n) क्योंकि पहले element search होता है, फिर remove किया जाता है।

4. contains() Method in ArrayList

contains() method check करता है कि कोई specific element list में मौजूद है या नहीं। यह boolean value (true या false) return करता है।

Example:

ArrayList<String> colors = new ArrayList<>();
colors.add("Red");
colors.add("Green");
colors.add("Blue");
System.out.println(colors.contains("Green")); // Output: true

How contains() Works Internally

यह method internally indexOf() method use करता है जो पूरे list को scan करता है जब तक matching element न मिल जाए। अगर element मिलता है तो true return होता है, वरना false।

Time Complexity of contains()

Operation Time Complexity Explanation
contains(Object o) O(n) क्योंकि पूरा list iterate करना पड़ता है जब तक matching element न मिल जाए।

5. Comparison of All Core Operations

Method Average Time Complexity Worst Case Use Case
add(E element) O(1) O(n) End में नया element जोड़ने के लिए।
add(int index, E element) O(n) O(n) Specific position पर insert करने के लिए।
get(index) O(1) O(1) किसी भी element को direct access करने के लिए।
remove(int index) O(n) O(n) Index के अनुसार element delete करने के लिए।
contains(Object o) O(n) O(n) किसी element के presence को check करने के लिए।

6. Performance Notes (Exam Useful)

  • ArrayList random access के लिए perfect है लेकिन insertion और deletion के लिए नहीं।
  • अगर बार-बार insertion या deletion करनी हो तो LinkedList बेहतर होती है।
  • add() operation सबसे efficient होता है जब end में elements जोड़ते हैं।
  • contains() या remove() large data sets में slow हो सकते हैं क्योंकि दोनों O(n) हैं।
  • ArrayList का internal array resize होता है — जिससे कभी-कभी performance impact होता है।

7. Exam Important Points (Notes)

  • add() – Average O(1), Worst O(n)
  • get() – Always O(1)
  • remove() – O(n) (shift operation involved)
  • contains() – O(n) (linear search)
  • ArrayList internally dynamic array use करता है।
  • Random access fast है क्योंकि indexing direct होती है।
  • Insertion और deletion costly हैं क्योंकि shifting required होती है।
  • अगर performance critical application है, तो LinkedList prefer करें।
  • Exam में अक्सर पूछा जाता है — “Why ArrayList is faster in get() operation?”

8. Real-Life Example

मान लो आपको किसी shopping cart app में items store करने हैं — वहाँ आप ArrayList use कर सकते हो क्योंकि items को बार-बार read (get) करना होता है। लेकिन अगर हर second items add/remove हो रहे हैं, तो LinkedList बेहतर होगी क्योंकि shifting cost ज़्यादा नहीं पड़ेगी।

9. Final Key Points (Short Notes)

  • ArrayList dynamic array पर आधारित होती है।
  • Fast random access, slow insertion/deletion।
  • add() → O(1)*, get() → O(1), remove() → O(n), contains() → O(n)
  • Capacity बढ़ने पर ArrayList internally resize करती है।
  • Exam में हमेशा पूछा जाता है: Time complexity of core operations