Problem: How to remove duplicate elements from the array
Solution:
内容: 1: Create a file & Declare a Class 2:声明主要方法 3:声明变量 4:接受输入 5:将输入传递给第二种方法 6:声明包含我们逻辑的第二种方法 7:从数组中删除重复项的实际逻辑 8:返回纯数组 9:显示输出
注意:我在此解决方案中遵循Java命名约定。 It is not forced to follow, but it is a good habit as a programmer. And it will make your program easy readable for other programmers.
1. Create a file & Declare a Class
建立档案EliminateDuplicatesFromArray.java 打开文件并声明类
import java.util.Scanner;
class EliminateDuplicatesFromArray{
}
根据Java约定,文件名和类名必须相同,这使得维护和组织程序更加容易。 We are importing the Scanner class to take inputs
2 : Declare the main method
public static void main(String as[]){
}
这是任何Java程序的启动方法
3 : Declare Variables in main method
现在声明程序中需要的必要变量。
- Two Arrays one for input and another for output
int[] list,pureList;
- Size of Array
int len;
- object of Scanner class
Scanner s=new Scanner(System.in);
这将要求我们是否使用动态初始化
4 : Taking inputs
输入/初始化数组的两种方法
- Static Initialization
list={10,20,30,30,30,30,40,50,10,40,10,50,10,0,0,0,10,70,80,10,90,10,90,90,90};
- Dynamic Initialization For this, we need to take input from the user.So we need an Object of Scanner class to get input as declared in step 3.
System.out.println("Enter Size of array");
int len=s.nextInt();
list=new int[len]; // instantiation
for(int i=0;i<list.length;i++)
{
System.out.print("Enter "+i+" element :=> ");
list[i]=s.nextInt(); // initialization
}
5 : Passing input to the second method
pureList=removeDuplicates(list);
通过清单作为方法的输入removeDuplicates它会返回到pureList。
6 : Declare the second method
创建第二种方法
public static int[] eliminateDuplicates(int[] list){
}
为什么静态的? 因为我们从main方法调用它。
7 : Actual logic for removing duplicates from an array
- 在中声明变量消除重复 method
int key,newSize;
int[] pureList;
的键variableisforselectingeachelementof的清单如键并比较键 witheachelementof的清单 pureListisforstoringpureArray newSizeis的lengthofpureListArray
7.1 : Initialize inputs
newSize=list.length;
list变量将包含传递的不纯数组 目前,newSize将包含输入数组的长度(不纯数组) newSize将随着元素的删除而减少。
7.2 : Process the Array [Actual Logic]
for(int i=0;i<newSize-1;i++)//for select each element as key
{
key=list[i];//store first element as Key
for(int j=i+1;j<newSize;j++){/* Start Comparing with Next Element
otherwise key element will be removed */
if(key==list[j])
{
newSize=newSize-1; /* Decreasing Length to Skip the Last
element which already shifted up
after below loop */
for(int k=j;k<newSize;k++)//Shift Up Array to remove Duplicate {
list[k]=list[k+1]; //remove duplicate
}
j--; //for skipped element after deletion
}
}
}
- 1st For loop Selecting Each element of the array Assigning to 键变量2nd For loop Selecting Each element of the array after the 键 elementIf Statement Comparing 键 with each element of array excluding 键 If match found then decrement newSize变量 because in 3rd loop we are deleting one element from the array3rd For loop removing an element from the array by shifting up the next element
现在递减第二循环计数器Ĵ删除一个元素是因为删除元素后,下一个元素的索引将成为已删除元素的索引。 因此,不会跳过下一个元素进行比较。
8 : Returning a pure array
在这之后清单没有重复值,但包含虚拟值 So Copy 清单 with newSize长度pureList数组。
pureList=new int[newSize]; //Instantiate pureList Array with newSize
for(int p=0;p<newSize;p++)
{
pureList[p]=list[p];
}
return pureList;
9 : Display the Output
for(int x: pureList)//for each loop for display output
{
System.out.println(x);
}