Compare elements in an array for duplicates

后端 未结 8 796
轻奢々
轻奢々 2020-12-21 00:34

I am trying to generate a 5 digit int array in Java and am having trouble on where to start. None of the numbers in the array can be duplicates. I can generate

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-21 00:59

    As an alternative, just sort the array and iterate it.

        List myList = new List() { 1, 1, 2, 3, 4, 5, 5, 7 , 1, 7};
        myList.Sort();
        for (int i = myList.Count - 1; i > 0; i--)
        {
            if (myList[i] == myList[i - 1])
                myList.RemoveAt(i);
        }
    

    But of course it's best not to get any duplicates to start with.

提交回复
热议问题