diff --git a/src/zabbix_server/lld/lld_host.c b/src/zabbix_server/lld/lld_host.c
index 9631cde8003..ba611ed9257 100644
--- a/src/zabbix_server/lld/lld_host.c
+++ b/src/zabbix_server/lld/lld_host.c
@@ -697,16 +697,19 @@ static void	lld_hosts_validate(zbx_vector_ptr_t *hosts, char **error)
 {
 	zbx_db_result_t		result;
 	zbx_db_row_t		row;
-	int			i, j;
-	zbx_lld_host_t		*host, *host_b;
+	int			i;
+	zbx_lld_host_t		*host;
 	zbx_vector_uint64_t	hostids;
 	zbx_vector_str_t	tnames, vnames;
+	zbx_hashset_t		hostnames;
 
 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
 
 	zbx_vector_uint64_create(&hostids);
 	zbx_vector_str_create(&tnames);		/* list of technical host names */
 	zbx_vector_str_create(&vnames);		/* list of visible host names */
+	zbx_hashset_create(&hostnames, hosts->values_num, ZBX_DEFAULT_STRING_PTR_HASH_FUNC,
+			ZBX_DEFAULT_STR_COMPARE_FUNC);
 
 	/* checking a host name validity */
 	for (i = 0; i < hosts->values_num; i++)
@@ -775,73 +778,67 @@ static void	lld_hosts_validate(zbx_vector_ptr_t *hosts, char **error)
 	/* checking duplicated host names */
 	for (i = 0; i < hosts->values_num; i++)
 	{
+		int	num_data = hostnames.num_data;
+
 		host = (zbx_lld_host_t *)hosts->values[i];
 
 		if (0 == (host->flags & ZBX_FLAG_LLD_HOST_DISCOVERED))
 			continue;
 
+		zbx_hashset_insert(&hostnames, &host->host, sizeof(&host->host));
+
 		/* only new hosts or hosts with changed host name will be validated */
 		if (0 != host->hostid && 0 == (host->flags & ZBX_FLAG_LLD_HOST_UPDATE_HOST))
 			continue;
 
-		for (j = 0; j < hosts->values_num; j++)
-		{
-			host_b = (zbx_lld_host_t *)hosts->values[j];
-
-			if (0 == (host_b->flags & ZBX_FLAG_LLD_HOST_DISCOVERED) || i == j)
-				continue;
-
-			if (0 != strcmp(host->host, host_b->host))
-				continue;
+		if (num_data != hostnames.num_data)
+			continue;
 
-			*error = zbx_strdcatf(*error, "Cannot %s host:"
-					" host with the same name \"%s\" (\"%s\") already exists.\n",
-					(0 != host->hostid ? "update" : "create"), host->host, host->name);
+		*error = zbx_strdcatf(*error, "Cannot %s host:"
+				" host with the same name \"%s\" (\"%s\") already exists.\n",
+				(0 != host->hostid ? "update" : "create"), host->host, host->name);
 
-			if (0 != host->hostid)
-			{
-				lld_field_str_rollback(&host->host, &host->host_orig, &host->flags,
-						ZBX_FLAG_LLD_HOST_UPDATE_HOST);
-			}
-			else
-				host->flags &= ~ZBX_FLAG_LLD_HOST_DISCOVERED;
+		if (0 != host->hostid)
+		{
+			lld_field_str_rollback(&host->host, &host->host_orig, &host->flags,
+					ZBX_FLAG_LLD_HOST_UPDATE_HOST);
 		}
+		else
+			host->flags &= ~ZBX_FLAG_LLD_HOST_DISCOVERED;
 	}
 
+	zbx_hashset_clear(&hostnames);
 	/* checking duplicated visible host names */
 	for (i = 0; i < hosts->values_num; i++)
 	{
+		int	num_data = hostnames.num_data;
+
 		host = (zbx_lld_host_t *)hosts->values[i];
 
 		if (0 == (host->flags & ZBX_FLAG_LLD_HOST_DISCOVERED))
 			continue;
 
+		zbx_hashset_insert(&hostnames, &host->name, sizeof(&host->name));
+
 		/* only new hosts or hosts with changed visible name will be validated */
 		if (0 != host->hostid && 0 == (host->flags & ZBX_FLAG_LLD_HOST_UPDATE_NAME))
 			continue;
 
-		for (j = 0; j < hosts->values_num; j++)
-		{
-			host_b = (zbx_lld_host_t *)hosts->values[j];
-
-			if (0 == (host_b->flags & ZBX_FLAG_LLD_HOST_DISCOVERED) || i == j)
-				continue;
-
-			if (0 != strcmp(host->name, host_b->name))
-				continue;
+		if (num_data != hostnames.num_data)
+			continue;
 
-			*error = zbx_strdcatf(*error, "Cannot %s host:"
-					" host with the same visible name \"%s\" already exists.\n",
-					(0 != host->hostid ? "update" : "create"), host->name);
+		*error = zbx_strdcatf(*error, "Cannot %s host:"
+				" host with the same visible name \"%s\" already exists.\n",
+				(0 != host->hostid ? "update" : "create"), host->name);
 
-			if (0 != host->hostid)
-			{
-				lld_field_str_rollback(&host->name, &host->name_orig, &host->flags,
-						ZBX_FLAG_LLD_HOST_UPDATE_NAME);
-			}
-			else
-				host->flags &= ~ZBX_FLAG_LLD_HOST_DISCOVERED;
+		if (0 != host->hostid)
+		{
+			lld_field_str_rollback(&host->name, &host->name_orig, &host->flags,
+					ZBX_FLAG_LLD_HOST_UPDATE_NAME);
 		}
+		else
+			host->flags &= ~ZBX_FLAG_LLD_HOST_DISCOVERED;
+
 	}
 
 	/* checking duplicated host names and visible host names in DB */
@@ -957,6 +954,7 @@ static void	lld_hosts_validate(zbx_vector_ptr_t *hosts, char **error)
 	zbx_vector_str_destroy(&vnames);
 	zbx_vector_str_destroy(&tnames);
 	zbx_vector_uint64_destroy(&hostids);
+	zbx_hashset_destroy(&hostnames);
 
 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __func__);
 }
