# How can I simplify my code when checking if a map has a key?

**URL:** <https://forums.unrealengine.com/t/how-can-i-simplify-my-code-when-checking-if-a-map-has-a-key/1205518>\
**Category:** Verse\
**Tags:** unreal-editor-for-fortnite, fortnite-creative, question, fortnite\
**Created:** [June 20, 2023, 6:43pm UTC](https://forums.unrealengine.com/t/how-can-i-simplify-my-code-when-checking-if-a-map-has-a-key/1205518 "2023-06-20T18:43:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![GarettHunter](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/garetthunter/32/719749_2.png) [@GarettHunter](https://forums.unrealengine.com/u/GarettHunter)\
**Post date:** [June 20, 2023, 6:43pm UTC](https://forums.unrealengine.com/t/how-can-i-simplify-my-code-when-checking-if-a-map-has-a-key/1205518/1 "2023-06-20T18:43:39Z")

</div>

I have an if-else pattern, but I don’t know how to write the if statement in order to negate it. The code in the else block is what matters to the function.

```auto
    # The players to keep alive and avoid elimination. The int value is unused.
    var SurvivingPlayers : [player]int = map{}

    EliminateNonSurvivors():void=
        for(Player : GetPlayspace().GetPlayers(), Character := Player.GetFortCharacter[], Character.IsActive[]):
            if(UnusedValue := SurvivingPlayers[Player]):
                var nothing : int = 0 # Does nothing, just to keep the if-else pattern.
            else:
                Character.SetVulnerability(true)
                Character.Damage(99999.9)

```

How can I clean up my code? Thanks 🙂

---

<div class="post-metadata">

**Author:** ![PiEqualsThree3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@PiEqualsThree3](https://forums.unrealengine.com/u/PiEqualsThree3)\
**Post date:** [June 20, 2023, 7:18pm UTC](https://forums.unrealengine.com/t/how-can-i-simplify-my-code-when-checking-if-a-map-has-a-key/1205518/2 "2023-06-20T19:18:03Z")

</div>

Is there a specific reason you are using a map over an array? With an array, you can use the (A : []t).Find[t] function which returns the index, or fails if the element t is not in the array. So you could do

```auto
var SurvivingPlayers : []player = array{}
for ( Player : GetPlayspace().GetPlayers()):
    # If a player is not in the array, the Find() function will fail, however 
    # we negate it using "not", meaning that the if will only execute if the 
    # Player is not in the SurvivingPlayers array
    if (not SurvivingPlayers.Find[Player]):
        # Damage code here

```

---

<div class="post-metadata">

**Author:** ![GarettHunter](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/garetthunter/32/719749_2.png) [@GarettHunter](https://forums.unrealengine.com/u/GarettHunter)\
**Post date:** [June 20, 2023, 7:38pm UTC](https://forums.unrealengine.com/t/how-can-i-simplify-my-code-when-checking-if-a-map-has-a-key/1205518/3 "2023-06-20T19:38:26Z")

</div>

Thinking about it now, I was concerned about the quadratic time complexity of searching an array in a for loop, but since it’s only like max 100 players in the worst case. I think using an array would be okay because I am not calling this function often.

However, I can imagine encountering future similar situations where I need to iterate often and be concerned about performance. I was hoping there was a Set or Hash like data structure, but I think a Map is the closest to having a fast lookup.

---

<div class="post-metadata">

**Author:** ![PiEqualsThree3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@PiEqualsThree3](https://forums.unrealengine.com/u/PiEqualsThree3)\
**Post date:** [June 20, 2023, 8:30pm UTC](https://forums.unrealengine.com/t/how-can-i-simplify-my-code-when-checking-if-a-map-has-a-key/1205518/4 "2023-06-20T20:30:43Z")

</div>

That makes sense. In that case, why not make use of the value by converting it into a logic type, and including all players inside the map. Then you can set the logic value to true or false if you want to eliminate them or not.

---

<div class="post-metadata">

**Author:** ![GarettHunter](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/garetthunter/32/719749_2.png) [@GarettHunter](https://forums.unrealengine.com/u/GarettHunter)\
**Post date:** [June 20, 2023, 8:50pm UTC](https://forums.unrealengine.com/t/how-can-i-simplify-my-code-when-checking-if-a-map-has-a-key/1205518/5 "2023-06-20T20:50:01Z")

</div>

Thanks, I think that solves it for this specific case.

---

<div class="post-metadata">

**Author:** ![ManualPotato](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/manualpotato/32/1144902_2.png) [@ManualPotato](https://forums.unrealengine.com/u/ManualPotato)\
**Post date:** [June 20, 2023, 10:51pm UTC](https://forums.unrealengine.com/t/how-can-i-simplify-my-code-when-checking-if-a-map-has-a-key/1205518/6 "2023-06-20T22:51:10Z")

</div>

You do the original request like so:

```auto
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

game_device := class(creative_device):

    Map: [int]int = map{}

    OnBegin<override>()<suspends>:void=

        if(not Map[0]):

            Print("No value")

```

---

<div class="post-metadata">

**Author:** ![GarettHunter](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/garetthunter/32/719749_2.png) [@GarettHunter](https://forums.unrealengine.com/u/GarettHunter)\
**Post date:** [June 21, 2023, 12:11am UTC](https://forums.unrealengine.com/t/how-can-i-simplify-my-code-when-checking-if-a-map-has-a-key/1205518/7 "2023-06-21T00:11:43Z")

</div>

Thanks, this seems to be exactly what I was looking for.

---

<div class="post-metadata">

**Author:** ![Knight\_Breaker](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/knight_breaker/32/1194711_2.png) [@Knight\_Breaker](https://forums.unrealengine.com/u/Knight_Breaker)\
**Post date:** [June 21, 2023, 6:07am UTC](https://forums.unrealengine.com/t/how-can-i-simplify-my-code-when-checking-if-a-map-has-a-key/1205518/8 "2023-06-21T06:07:43Z")

</div>

> [@GarettHunter](#):
>
> I was concerned about the quadratic time complexity

How would this be quadratic? The time complexity for a simple for iteration would be linear, therefore O(N) in the worst case. The `map` is similar to a `dictionary` from other languages and I would strongly assume that a single lookup is of constant complexity O(1).

However the map’s key type is only `comparable` (aka. equatable) and not something like “hashable”. So the lookup might actually not be O(1) after all.

---

<div class="post-metadata">

**Author:** ![GarettHunter](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/garetthunter/32/719749_2.png) [@GarettHunter](https://forums.unrealengine.com/u/GarettHunter)\
**Post date:** [June 21, 2023, 6:38am UTC](https://forums.unrealengine.com/t/how-can-i-simplify-my-code-when-checking-if-a-map-has-a-key/1205518/9 "2023-06-21T06:38:39Z")

</div>

You are right, especially considering the way I worded the following:

```auto
I was concerned about the quadratic time complexity of searching an array in a for loop

```

Maybe I should have worded it like:

```auto
I was concerned about the quadratic time complexity of calling the Find function for every iteration in the for loop.

```

Assuming Find is O(N).

I never was good at English. Thanks for helping me improve the way I word things.

My assumption is that Find is O(N), and then being called in an O(M) for loop is O(NM) being quadratic. Maybe I’m having a stroke or something, but that’s how I assume it was quadratic.

Thanks. I welcome more corrections. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [July 21, 2023, 6:39am UTC](https://forums.unrealengine.com/t/how-can-i-simplify-my-code-when-checking-if-a-map-has-a-key/1205518/10 "2023-07-21T06:39:15Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
