# Infinite array

**URL:** <https://discourse.julialang.org/t/infinite-array/128455>\
**Category:** New to Julia\
**Created:** [April 27, 2025, 6:18am UTC](https://discourse.julialang.org/t/infinite-array/128455 "2025-04-27T06:18:33Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [April 27, 2025, 6:18am UTC](https://discourse.julialang.org/t/infinite-array/128455/1 "2025-04-27T06:18:34Z")

</div>

Hello,  
How can I define an infinite array? For example, I don’t know the number of data that will be entered into the array and I want to have an infinite array where no matter how much data I put into it, the length of the array will be the same number of data.

Thank you.

---

<div class="post-metadata">

**Author:** ![hz-xiaxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hz-xiaxz/32/209585_2.png) [@hz-xiaxz](https://discourse.julialang.org/u/hz-xiaxz)\
**Post date:** [April 27, 2025, 6:38am UTC](https://discourse.julialang.org/t/infinite-array/128455/2 "2025-04-27T06:38:59Z")

</div>

why not define a empty `Array` or `Vector`, and `push!` the elements?

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [April 27, 2025, 6:39am UTC](https://discourse.julialang.org/t/infinite-array/128455/3 "2025-04-27T06:39:35Z")

</div>

An array that can truly hold infinitely many things is impossible on a computer with finite memory/storage.

Is what you need a way to dynamically increase the size of an array? Then you could have a look at the functions `push!`, `resize!`, and `append!` for example. It is fairly common that the final size of an array is not known at the time of creation, so what you could do is something like

```julia
data = [] # If you know the type of the elements, it's usually better to specify it here

userInput = ...

append!(data, userInput)

```

Julia automatically takes care that the array `data` is large enough to hold everything you want to put into it. But it is not “infinite” since you will run out of memory at some point and get an error, e.g. here I’m trying to ask for more space than my machine actually has:

```julia-repl
julia> data = Array{Int, 1}(undef, 1000000000000000000)
ERROR: OutOfMemoryError()
Stacktrace:
 [1] Vector{Int64}(::UndefInitializer, m::Int64)
   @ Core ./boot.jl:477
 [2] top-level scope
   @ REPL[9]:1

```

I’m sure there are better ways than this, but maybe you could specify what you need exactly a bit more in detail.

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [April 27, 2025, 8:30am UTC](https://discourse.julialang.org/t/infinite-array/128455/4 "2025-04-27T08:30:56Z")

</div>

Hello,  
Thank you so much for your reply.  
I did:

```julia
function duplicate()
    array = [1,2,3,4,4,4,5]
    dup = []
    counter = 1
    for i = 1 : (length(array)-1)
        for j = 1 : i
            if array[j] == array[i+1]
                dup[counter] = array[j]
                counter += 1
            end
        end
    end
print(dup)
end
duplicate()

```

But I got:

```julia
ERROR: LoadError: BoundsError: attempt to access 0-element Vector{Any} at index [1]

```

---

<div class="post-metadata">

**Author:** ![Eliassj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eliassj/32/204117_2.png) [@Eliassj](https://discourse.julialang.org/u/Eliassj)\
**Post date:** [April 27, 2025, 8:38am UTC](https://discourse.julialang.org/t/infinite-array/128455/5 "2025-04-27T08:38:46Z")

</div>

In Julia arrays are not automatically extended when you add something to a nonexistent index. You should do:

```julia
push!(dup, array[j])

```

This will extend `dup` by 1 and add `array[j]`. If you want to add several items at once you should use `append!`. See [the docs](https://docs.julialang.org/en/v1/base/collections/#Base.push!)

---

<div class="post-metadata">

**Author:** ![hz-xiaxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hz-xiaxz/32/209585_2.png) [@hz-xiaxz](https://discourse.julialang.org/u/hz-xiaxz)\
**Post date:** [April 27, 2025, 8:41am UTC](https://discourse.julialang.org/t/infinite-array/128455/6 "2025-04-27T08:41:33Z")

</div>

Hi, thanks for your attempt and welcome to the community!  
If you create an empty array using `dup=[]`, julia doesn’t automatically index it. If you are assigning elements sequentially, I would advise using

```julia
push!(dup, array[j])

```

instead of your previous attempt. If you want to push many elements in one time, please use `append!()` etc.  
You can also assign the empty array with a large number, like

```julia
dup = zeros(Int64, 100)

```

That would create an indexed 100 element array with default number 0.

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [April 27, 2025, 9:38am UTC](https://discourse.julialang.org/t/infinite-array/128455/7 "2025-04-27T09:38:15Z")

</div>

While I would also advise you to use `push!` etc. on `Vector`s, you could alternatively use [dictionaries](https://docs.julialang.org/en/v1/base/collections/#Dictionaries).

```julia
function duplicate()
    array = [1,2,3,4,4,4,5]
    dup = Dict() # or Dict{Int, Int}() for better performance
    counter = 1
    for i = 1 : (length(array)-1)
        for j = 1 : i
            if array[j] == array[i+1]
                dup[counter] = array[j]
                counter += 1
            end
        end
    end
    return dup
end

dup = duplicate() # (Note that the entries in dup are stored in arbitrary order)
for i = 1:length(dup)
    println(dup[i]) 
end

```

```julia-repl
4
4
4

```

This will be less performant than using `Vector`s, but that’s presumably not really a concern at this point. And obviously you will get a `Dict`, not an `Array`. But this would allow you to assign to entries which do not exist yet.

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [April 27, 2025, 12:48pm UTC](https://discourse.julialang.org/t/infinite-array/128455/8 "2025-04-27T12:48:52Z")

</div>

Independent of what `duplicate` is actually supposed to do (not sure if the name communicates well what it actually does), [comprehensions](https://docs.julialang.org/en/v1/manual/arrays/#man-comprehensions) are very convenient for building new arrays (also of unknown size):

```julia
function duplicate(array)
    [array[j] 
     for i in 1:(length(array)-1) 
     for j in 1:i
     if array[j] == array[i+1]]
end

```

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [April 28, 2025, 12:09pm UTC](https://discourse.julialang.org/t/infinite-array/128455/9 "2025-04-28T12:09:32Z")

</div>

Hello,  
Thank you so much for you reply.  
Is `Dict` much less efficient than array?

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [April 28, 2025, 6:09pm UTC](https://discourse.julialang.org/t/infinite-array/128455/10 "2025-04-28T18:09:56Z")

</div>

This all depends on the context. You can time and check allocated memory yourself using among others `@time`, BenchmarkTools.jl’s `@btime`, `@benchmark`, or Chairmark.jl’s `@b`:

> **Method definitions**
>
> ```julia
> using BenchmarkTools
> 
> function duplicate_preallocated()
> array = [1,2,3,4,4,4,5]
> dup = zeros(Int, length(array))
> counter = 1
> for i = 1 : (length(array)-1)
> for j = 1 : i
> if array[j] == array[i+1]
> dup[counter] = array[j]
> counter += 1
> end
> end
> end
> return dup
> end
> 
> function duplicate_push() # cf. hz-xiaxz, Eliassj
> array = [1,2,3,4,4,4,5]
> dup = Int[] # could use sizehint! for potentially slightly better performance
> for i = 1 : (length(array)-1)
> for j = 1 : i
> if array[j] == array[i+1]
> push!(dup, array[j])
> end
> end
> end
> return dup
> end
> 
> function duplicate_comprehension() # cf. bertschi
> array = [1,2,3,4,4,4,5]
> return [array[j] 
> for i in 1:(length(array)-1) 
> for j in 1:i
> if array[j] == array[i+1]]
> end
> 
> function duplicate_dict() 
> array = [1,2,3,4,4,4,5]
> dup = Dict{Int, Int}()
> counter = 1
> for i = 1 : (length(array)-1)
> for j = 1 : i
> if array[j] == array[i+1]
> dup[counter] = array[j]
> counter += 1
> end
> end
> end
> return dup
> end
> 
> ```

```julia-repl
julia> @btime duplicate_preallocated()
  47.172 ns (4 allocations: 224 bytes)
7-element Vector{Int64}:
 4
 4
 4
 0
 0
 0
 0

julia> @btime duplicate_push()
  47.730 ns (3 allocations: 208 bytes)
3-element Vector{Int64}:
 4
 4
 4

julia> @btime duplicate_comprehension()
  73.975 ns (5 allocations: 272 bytes)
3-element Vector{Int64}:
 4
 4
 4

julia> @btime duplicate_dict()
  109.903 ns (6 allocations: 560 bytes)
Dict{Int64, Int64} with 3 entries:
  2 => 4
  3 => 4
  1 => 4

```

If I were to phrase these results as the `Dict` approach being more than twice as slow as the `push!` version, you might conclude that it is much less efficient. If you needed to run this function many times for different (small) input `array`s, this would certainly be a valid conclusion.

Alternatively, you might say that using `Dict`s here is only some 60 ns slower, which is completely negligible. This conclusion would be equally valid, if you would only need to run this function once.

---

<div class="post-metadata">

**Author:** ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Post date:** [April 28, 2025, 8:12pm UTC](https://discourse.julialang.org/t/infinite-array/128455/11 "2025-04-28T20:12:33Z")

</div>

ElasticArrays.jl ??
