How can I remove duplicate elements from a given array in java without using collections

后端 未结 10 790
逝去的感伤
逝去的感伤 2021-01-06 02:20

I have an array elements like this:

int arr[] = {1,1,2,2,3,3,4,4};

I want to remove the duplicate elements from. Searched on the internet

10条回答
  •  伪装坚强ぢ
    2021-01-06 03:01

        int flag = 0, k = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arrAns.length; j++) {
                if (arr[i] == arrAns[j]) {
                    flag = 0;
                    break;
                }
                flag=1;
            }
            if (flag == 1) {
                arrAns[k] = arr[i];
                k++;
            }
            flag = 0;
        }
    

提交回复
热议问题