public static void main(String[] args) {
String[] strArray = { "Islamabad", "Gilgit", "Nawabshah", "Karachi",
"Abbotabad", "Gilgit", "Hyderabad", "Islamabad", "Lahore",
"Hyderabad", "Sukkur", "Faisalabad", "Kohat", "Faisalabad",
"Faisalabad", "Bhakkar", "Faisalabad", "Lahore", "Abbotabad",
"Attock", "Karachi", "Rawalpindi", "Nawab Shah", "Abbotabad",
"Sukkur", "Attock", "Multan", "Faisalabad", "Multan", "Sukkur" };
System.out.println("Length : " + strArray.length);
Arrays.sort(strArray);
for (int i = 0; i < strArray.length - 1; i++) {
for (int j = i + 1; j < strArray.length; j++) {
if ((strArray[i].equals(strArray[j])) && (i != j)) {
System.out.println("Duplicate Element is : " + strArray[j]);
}
}
}
HashSet<String> set = new HashSet<String>();
for (int i = 0; i < strArray.length; i++) {
// If same integer is already present then add method will return
// FALSE
if (set.add(strArray[i]) == false) {
System.out.println("Duplicate element found : " + strArray[i]);
}
}
}
I have tried both methods following this but none of them give any accurate result, it doesn't work for the my number of array elements查找重複的字符串數組中的元素與在Java計數elecment的Occurence
該鏈接使我無處 – nullpointer
它並沒有給我一個@nullpointer –
歡迎堆棧溢出!它看起來像你需要學習使用調試器。請幫助一些[互補調試技術](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。如果您之後仍然有問題,請隨時返回更多詳情。 –