Removing the duplicate elements from an array is the same as printing the unique elements of an array.
Notes:- If we write
boolean[ ] flag = new boolean[ size ] ;
in java, it creates an array of booleans where false present in all positions by default.
We don't have to assign value to flag array elements exclusively.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class RemoveDuplicate{ public static void main(String []args){ int[] arr = {1, 2, 3, 6, 3, 3, 3, 4, 2}; boolean[] flag = new boolean[arr.length]; for(int i=0; i<arr.length; i++) if(!flag[i]) for(int j=i+1; j<arr.length; j++) if(arr[i]==arr[j]) flag[j] = true; for(int i=0; i<arr.length; i++) if(!flag[i]) System.out.print(arr[i]+" "); } } |
Python
arr = [1, 2, 3, 6, 3, 3, 3, 4, 2] flag = [False]*len(arr) for i in range(0,len(arr)): if not flag[i]: for j in range(i+1,len(arr)): if arr[i]==arr[j]: flag[j] = True for i in range(0,len(arr)): if not flag[i]: print(arr[i],end=" ") |