Post

Array/ Array list/ Linked list

Screenshot 2024-07-24 at 10 46 24



How can we eradicate duplicated value in array?

1. Set

  • Set does not allow duplication
  • convert array into set

πŸ’‘ Set https://soheeparklee.github.io/posts/DS-list_set_map/

2. For loop

  • 배열을 λŒλ©΄μ„œ μ€‘λ³΅λ˜λŠ” κ°’ μ‚­μ œ

3. Array.sort

  • 배열을 μ˜€λ¦„μ°¨μˆœ/λ‚΄λ¦Όμ°¨μˆœμœΌλ‘œ μ •λ ¬
  • μ˜†μ— μžˆλŠ” 값끼리 λΉ„κ΅ν•΄μ„œ κ°™μœΌλ©΄ μ‚­μ œ

4. Stream API

  • use stream to delete duplicated value
  • stream API is added from JAVA 8



Array πŸ†š Array List πŸ†š Linked List

βœ”οΈ Array

  • πŸ‘πŸ» can access directly with index to element
    • time complexity: O(1)
  • πŸ‘ŽπŸ» need to fix size while creating


βœ”οΈ Array List(Array)

Screenshot 2024-08-20 at 01 17 55

  • use dynamic array to store data
  • πŸ‘πŸ» dynamic
  • πŸ‘πŸ» has list, can access directly
  • πŸ‘πŸ» fast in searching data
  • insertion, deletion slow


Why slow in insertion, deletion?

데이터 μΆ”κ°€ 및 μ‚­μ œ λ•Œ 데이터λ₯Ό μž¬ν• λ‹Ήν•œλ‹€.
thus, insertion and deletion slower than array

βœ”οΈ Linked List(Node)

Screenshot 2024-08-20 at 01 19 07

  • use doubly linked list to store data
  • πŸ‘πŸ» dynamic: do not need to fix size
  • πŸ‘πŸ» insertion, deletion easy
    • has address that saves the location of previous, next nodes
    • for insertion, deletion, just need to change value of previous, next node
  • πŸ‘ŽπŸ» cannot access directly, need to search from head

πŸ“Œ Q&A

Why use array, instead of linked list?

fast in search
can access element using index


When to use array, when to use linked list?

array: search
linked list: insertion, deletion

πŸ’‘ Reference

https://soheeparklee.github.io/posts/JAVA_list/
https://soheeparklee.github.io/posts/JAVA_collectionFramework/

This post is licensed under CC BY 4.0 by the author.