# Indexing arrays of strings (finding duplicates)

**URL:** <https://discourse.julialang.org/t/indexing-arrays-of-strings-finding-duplicates/53023>\
**Category:** New to Julia\
**Created:** [January 8, 2021, 2:32am UTC](https://discourse.julialang.org/t/indexing-arrays-of-strings-finding-duplicates/53023 "2021-01-08T02:32:03Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![graham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/graham/32/23781_2.png) [@graham](https://discourse.julialang.org/u/graham)\
**Post date:** [January 8, 2021, 2:32am UTC](https://discourse.julialang.org/t/indexing-arrays-of-strings-finding-duplicates/53023/1 "2021-01-08T02:32:03Z")

</div>

Hello all,

I’m looking to find duplicates within an array (an exercise from the Think Julia text).  
I’ve got it working for numerical values, but not sure how to generalize to strings as well. Parse? Find?

```julia
function hasduplicates(arr)
    arr2 = arr[:]

    if typeof(arr2[1]) == String
        for c in arr2
            arr2[c] = typeof(parse(Int64, arr2[c]))

        end
    end

    sort(arr2)

    for c in arr2
        if c < length(arr2) && arr2[c] ≡ arr2[c+1]
            return true
        end
    end
    false
end

hasduplicates(["hello", "goodbye"])

```

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [January 8, 2021, 2:39am UTC](https://discourse.julialang.org/t/indexing-arrays-of-strings-finding-duplicates/53023/2 "2021-01-08T02:39:37Z")

</div>

~~`nextind` might be suitable to search more elements in strings or arrays.~~

```julia
function hasdup(xs::AbstractVector)
  found = Set{Int}()
  for x in xs
    x = toint(x)
    x in found && return true
    push!(found, x)
  end
  false
end

 toint(x::Int) = x
 toint(x::String) = parse(Int, x)

```

---

<div class="post-metadata">

**Author:** ![graham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/graham/32/23781_2.png) [@graham](https://discourse.julialang.org/u/graham)\
**Post date:** [January 8, 2021, 2:45am UTC](https://discourse.julialang.org/t/indexing-arrays-of-strings-finding-duplicates/53023/3 "2021-01-08T02:45:14Z")

</div>

I’m getting errors to do with my use of `<` in my if statement. I might also be falling off the edge, but not sure how to handle the boolean expression first.

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [January 8, 2021, 3:01am UTC](https://discourse.julialang.org/t/indexing-arrays-of-strings-finding-duplicates/53023/4 "2021-01-08T03:01:33Z")

</div>

> [@graham](#):
>
> ```julia
> if typeof(arr2[1]) == String
> for c in arr2
> arr2[c] = typeof(parse(Int64, arr2[c]))
> 
> end
> end
> 
> ```

What do you mean by `typeof(arr2[1]) == String`?  
It seems that you didn’t properly consider the exercise. Local variable `c` here is likely to have `String` type(at least for `arr2[1]`), and you index an array with a string, which will constantly raise a TypeError.

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [January 8, 2021, 3:05am UTC](https://discourse.julialang.org/t/indexing-arrays-of-strings-finding-duplicates/53023/5 "2021-01-08T03:05:24Z")

</div>

What about the following approach?

```julia
function hasduplicates(arr::AbstractVector{Int64})
    # your original implementation
end

function hasduplicates(arr::AbstractVector{String}
    hasduplicates(parse.(Int64, arr))
end

```

---

<div class="post-metadata">

**Author:** ![graham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/graham/32/23781_2.png) [@graham](https://discourse.julialang.org/u/graham)\
**Post date:** [January 8, 2021, 3:35am UTC](https://discourse.julialang.org/t/indexing-arrays-of-strings-finding-duplicates/53023/6 "2021-01-08T03:35:29Z")

</div>

Yeah, I guess I’d add an if statement to determine if the elements are strings then parse them to allow the equivalency statement to work.

Thank you, makes much more sense!

Edit: the typeof(parse()) doesn’t make any sense for sure, just frustratingly copied some rando code I found!

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [January 8, 2021, 4:09am UTC](https://discourse.julialang.org/t/indexing-arrays-of-strings-finding-duplicates/53023/7 "2021-01-08T04:09:32Z")

</div>

If you have an array of strings and all you cared about is finding duplicates then you can do the following

```julia
julia> arr = ["apple","banana","cherry","banana"]
4-element Array{String,1}:
 "apple"
 "banana"
 "cherry"
 "banana"

julia> sorted_arr = sort(arr)
4-element Array{String,1}:
 "apple"
 "banana"
 "banana"
 "cherry"

julia> for k = 2:length(sorted_arr)
         if sorted_arr[k-1] == sorted_arr[k]
             println("Found a duplicate string at $(k-1)")
         end
       end
Found a duplicate string at 2

```

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [January 8, 2021, 4:26am UTC](https://discourse.julialang.org/t/indexing-arrays-of-strings-finding-duplicates/53023/8 "2021-01-08T04:26:07Z")

</div>

Why not just `hasduplicates(xs) = length(xs) != length(Set(xs))`?

---

<div class="post-metadata">

**Author:** ![graham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/graham/32/23781_2.png) [@graham](https://discourse.julialang.org/u/graham)\
**Post date:** [January 8, 2021, 4:26am UTC](https://discourse.julialang.org/t/indexing-arrays-of-strings-finding-duplicates/53023/9 "2021-01-08T04:26:56Z")

</div>

Cuz I dumb

---

<div class="post-metadata">

**Author:** ![healyp](https://avatars.discourse-cdn.com/v4/letter/h/67e7ee/32.png) [@healyp](https://discourse.julialang.org/u/healyp)\
**Post date:** [January 8, 2021, 7:52am UTC](https://discourse.julialang.org/t/indexing-arrays-of-strings-finding-duplicates/53023/10 "2021-01-08T07:52:27Z")

</div>

```julia
    sort(arr2)

    for c in arr2
     :

```

Note also that `arr2` will not be sorted. Use either `sort!(arr2)` or `arr3 = sort(arr2)`.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 8, 2021, 8:08am UTC](https://discourse.julialang.org/t/indexing-arrays-of-strings-finding-duplicates/53023/11 "2021-01-08T08:08:51Z")

</div>

Well, you could always do

```julia
hasduplicates(xs) = !allunique(xs)

```

This should be faster than `length(xs) != length(Set(xs))` too, since it would bail out early.

I don’t understand much of your code, though. It looks like it wouldn’t work for any strings except numerical strings, since you are doing `parse(Int, ...)`. Also, this

> [@graham](#):
>
> ```julia
> for c in arr2
> arr2[c] = typeof(parse(Int64, arr2[c]))
> 
> ```

Here, `c` is an _element_ of `arr2`, but then you use it as an _index_?
