Help porting Java code to Julia

## where i wrote java, i need to convert it to julia. 
##can some one tell me how ? thanks 

function MinimumUnique(arr::Vector{T}) where T<:Integer
   map = Dict{T,Int}()
   sum = 0
    for value in arr
      if !haskey(map, value)
        map[value]=1
            sum += value && continue
      end
      while haskey(map, value)
            value++
      end
      if !haskey(map, value)
              map[value]=1
                sum += value
                  println(value) && break
      end
    end
        return sum
end
input = [1 2 2]
println("Total pairs   " , MinimumUnique(arr))

You can look at the documentation about Dict

https://docs.julialang.org/en/v1/base/collections/#Base.Dict

there is a function name ‘haskey’ that is what you are looking for if you want to know if the key is in the dictionary…

best regards

2 Likes

Use for value in arr will be better.

1 Like

i tried to update all the thing that i consider it was wrong, but know i found some debbuging.

julia version 1.1.0
error during bootstrap:
LoadError("main.jl", 11, ErrorException("syntax: unexpected \"end\""))
rec_backtrace at /buildworker/worker/package_linux64/build/src/stackwalk.c:94
record_backtrace at /buildworker/worker/package_linux64/build/src/task.c:217 [inlined]
jl_throw at /buildworker/worker/package_linux64/build/src/task.c:417
jl_errorf at /buildworker/worker/package_linux64/build/src/rtutils.c:77
jl_toplevel_eval_flex at /buildworker/worker/package_linux64/build/src/toplevel.c:722
jl_parse_eval_all at /buildworker/worker/package_linux64/build/src/ast.c:883
jl_load at /buildworker/worker/package_linux64/build/src/toplevel.c:826
exec_program at /gocode/src/github.com/replit/prybar/languages/julia/pry.h:48
runtime.asmcgocall at /usr/lib/go-1.10/src/runtime/asm_amd64.s:688

the debbaging said that there is an end that is not excepted, put i not found it

This

sum += value && continue

and

println(value) && break

is wrong because sum+=value is not a boolean value.
The && is like

if(sum+=value) == true

Try

sum+=value
continue
1 Like
function MinimumUnique(arr::Vector{T}) where T<:Integer
   map = Dict{T,Int}()
   sum = 0
    for value in arr
      if !haskey(map, value)
        map[value]=1
            sum+=value 
            continue
      end
      while haskey(map, value)
            value++
      end
      if !haskey(map, value)
              map[value]=1
                sum += value
                  println(value)
                  break
      end
    end
        return sum
end
input = [1 2 2]
println("Total pairs   " , MinimumUnique(arr))

i fix it , its still the same error
after break or continue should came end ?
this is ok to write !haskey(map, value) - the ! so this is now if the value does not in the map ? thats write ?

You wrote

input = [1 2 2]
println("Total pairs   " , MinimumUnique(arr))

test it with input and not with arr :slight_smile:

1 Like

After changing some values it should work

function MinimumUnique(arr::AbstractArray{T}) where {T<:Integer}
   map = Dict{T,Int}()
   sum = 0
   for value in arr

      if !haskey(map, value)
            map[value]=1
            sum+=value
            continue
      end

      while haskey(map, value)
            value+=1
      end

      if !haskey(map, value)
            map[value]=1
            sum += value
            println(value)
            break
      end

    end
    return sum
end
input = [1 2 2]
println("Total pairs   " , MinimumUnique(input))
1 Like
function MinimumUnique(arr::AbstractArray{T}) where T<:Integer
   map = Dict{T,Int}()
   sum = 0
    for value in arr
      if !haskey(map, value)
        map[value]=1
            sum+=value 
          continue
      end
      while haskey(map, value)
            value+=1
      end
      if !haskey(map, value)
              map[value]=1
                sum += value
                continue
      end
    end
  return sum
end
arr = [1 2 2 2]
println("Total pairs   " , MinimumUnique(arr))

this is working , thank’s a lot

Ok fine :slight_smile:

++ is not working in Julia use +=1 instead

best regards

1 Like