C# 方法从数组中获取密钥

C# 方法从数组中获取密钥,c#,arrays,C#,Arrays,这个PHP脚本的等效C#语法是什么 <?php $arr = array("linux", "windows", "linux", "linux", "windows", "mac os", "unix", "mac os"); $unique = array_unique($arr); foreach($unique as $key=>$value){ echo $key."\n"; } ?> 因此,将删除阵列的重复项,然后显示阵列的键。我只能显示数组的值: str

这个PHP脚本的等效C#语法是什么

<?php
$arr = array("linux", "windows", "linux", "linux", "windows", "mac os", "unix", "mac os");
$unique = array_unique($arr);
foreach($unique as $key=>$value){
    echo $key."\n";
}
?>
因此,将删除阵列的重复项,然后显示阵列的键。我只能显示数组的值:

string[] arr = { "linux", "windows", "linux", "linux", "windows", "mac os", "unix", "mac os" };
string[] uniq = arr.Distinct().ToArray();
foreach (string unik in uniq)
{
    textBox1.AppendText(unik+"\r\n");
}

使用Linq可以相当轻松地做到这一点:

var indices = arr.Distinct()
                 .Select(s => Array.IndexOf(arr,s));

foreach (int i in indices)
{
    textBox1.AppendText(i+"\r\n");
}
或包含值和索引:

var indices = arr.Distinct()
                 .Select(s => new {s, i = Array.IndexOf(arr,s)});

foreach (var si in indices)
{
    textBox1.AppendText(string.Format({0}: {1}\n", si.i, si.s));
}
如果性能是一个问题,则更高效(尽管更难理解)的版本将是:

var indices = arr.Select((s, i) => new {s, i})  // select the value and the index
                 .GroupBy(si => si.s)           // group by the value
                 .Select(g => g.First());       // get the first value and index

foreach (var si in indices)
{
    textBox1.AppendText(string.Format({0}: {1}\n", si.i, si.s));
}

使用Linq可以相当轻松地做到这一点:

var indices = arr.Distinct()
                 .Select(s => Array.IndexOf(arr,s));

foreach (int i in indices)
{
    textBox1.AppendText(i+"\r\n");
}
或包含值和索引:

var indices = arr.Distinct()
                 .Select(s => new {s, i = Array.IndexOf(arr,s)});

foreach (var si in indices)
{
    textBox1.AppendText(string.Format({0}: {1}\n", si.i, si.s));
}
如果性能是一个问题,则更高效(尽管更难理解)的版本将是:

var indices = arr.Select((s, i) => new {s, i})  // select the value and the index
                 .GroupBy(si => si.s)           // group by the value
                 .Select(g => g.First());       // get the first value and index

foreach (var si in indices)
{
    textBox1.AppendText(string.Format({0}: {1}\n", si.i, si.s));
}
这对我很有用:

        string[] arr = { "linux", "windows", "linux", "linux", "windows", "mac os", "unix", "mac os" };
        string[] uniq = new string[0];
        string[] keys = new string[0];


        for (int i = 0; i < arr.Length; i++)
        {
            if (uniq.Contains(arr[i]))
            {
                continue;
            }
            else
            {
                uniq = uniq.Concat(new string[] { arr[i] }).ToArray();
                keys = keys.Concat(new string[] { i + "" }).ToArray();
            }
        }

        foreach (string key in keys)
        {
            textBox1.Append(key + "\r\n");
        }
string[]arr={“linux”、“windows”、“linux”、“linux”、“windows”、“mac os”、“unix”、“mac os”};
字符串[]uniq=新字符串[0];
字符串[]键=新字符串[0];
对于(int i=0;i
这对我很有用:

        string[] arr = { "linux", "windows", "linux", "linux", "windows", "mac os", "unix", "mac os" };
        string[] uniq = new string[0];
        string[] keys = new string[0];


        for (int i = 0; i < arr.Length; i++)
        {
            if (uniq.Contains(arr[i]))
            {
                continue;
            }
            else
            {
                uniq = uniq.Concat(new string[] { arr[i] }).ToArray();
                keys = keys.Concat(new string[] { i + "" }).ToArray();
            }
        }

        foreach (string key in keys)
        {
            textBox1.Append(key + "\r\n");
        }
string[]arr={“linux”、“windows”、“linux”、“linux”、“windows”、“mac os”、“unix”、“mac os”};
字符串[]uniq=新字符串[0];
字符串[]键=新字符串[0];
对于(int i=0;i
这里的LinQ解决方案-res包含“arr”字符串数组中第一次出现的索引:

string[] arr = { "linux", "windows", "linux", "linux", "windows", "mac os", "unix", "mac os" };

var res = arr.Select((value, index) => new { value, index })
          .ToDictionary(pair => pair.index, pair => pair.value)
          .GroupBy(x => x.Value)
          .Select(x => x.First().Key);

foreach (int i in res)
{
    textBox1.AppendText(i+"\r\n");
}

这里,LinQ解决方案-res包含“arr”字符串数组中第一次出现的索引:

string[] arr = { "linux", "windows", "linux", "linux", "windows", "mac os", "unix", "mac os" };

var res = arr.Select((value, index) => new { value, index })
          .ToDictionary(pair => pair.index, pair => pair.value)
          .GroupBy(x => x.Value)
          .Select(x => x.First().Key);

foreach (int i in res)
{
    textBox1.AppendText(i+"\r\n");
}

Distinct()
是否会导致索引从原始数组移动?这会在数组中搜索每个索引。当您可以在O(n)中执行此操作时,它是O(n^2)。@PinnyM No-
Distinct
不会以任何方式影响原始数组。@ZongZhengLi对于10个值来说,这应该不是问题。但是公平地说,我将添加一个更高性能的版本。我不相信数组有IndexOf()。列表没有,但没有System.Array。您需要使用
Array.IndexOf(arr,s)
Distinct()
会不会导致索引从原始数组移动?这会在数组中搜索每个索引。当您可以在O(n)中执行此操作时,它是O(n^2)。@PinnyM No-
Distinct
不会以任何方式影响原始数组。@ZongZhengLi对于10个值来说,这应该不是问题。但为了公平起见,我将添加一个更高性能的版本。我不相信数组有IndexOf()。列表没有,但没有System.Array。您需要使用
Array.IndexOf(arr,s)
。而不是需要字典的数组。然后
包含
和添加键/值都是O(1)而不是O(n)。无论如何,您可能不想附加到这样的数组中,只需使用一个列表(在本例中是另一个集合)作为开始。它也适用于
list
collection。我只想让它尽可能简单。它可以工作,但随着列表以指数速度越来越大,它会越来越慢。你想要的不是数组,而是字典。然后
包含
和添加键/值都是O(1)而不是O(n)。无论如何,您可能不想附加到这样的数组中,只需使用一个列表(在本例中是另一个集合)作为开始。它也适用于
list
collection。我只想让它尽可能简单。它是有效的,但随着列表以指数级的速度越来越大,它会越来越慢。