Skip to content

Commit 5174aeb

Browse files
Resolve #7253: Clean up duplicate Java algorithm implementations
1 parent 3e36833 commit 5174aeb

File tree

17 files changed

+243
-312
lines changed

17 files changed

+243
-312
lines changed

DIRECTORY.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
- 📄 [FirstDifferentBit](src/main/java/com/thealgorithms/bitmanipulation/FirstDifferentBit.java)
4444
- 📄 [GenerateSubsets](src/main/java/com/thealgorithms/bitmanipulation/GenerateSubsets.java)
4545
- 📄 [GrayCodeConversion](src/main/java/com/thealgorithms/bitmanipulation/GrayCodeConversion.java)
46-
- 📄 [HammingDistance](src/main/java/com/thealgorithms/bitmanipulation/HammingDistance.java)
4746
- 📄 [HigherLowerPowerOfTwo](src/main/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwo.java)
4847
- 📄 [HighestSetBit](src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java)
4948
- 📄 [IndexOfRightMostSetBit](src/main/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBit.java)
@@ -346,7 +345,6 @@
346345
- 📄 [LongestIncreasingSubsequence](src/main/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequence.java)
347346
- 📄 [LongestIncreasingSubsequenceNLogN](src/main/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequenceNLogN.java)
348347
- 📄 [LongestPalindromicSubsequence](src/main/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubsequence.java)
349-
- 📄 [LongestPalindromicSubstring](src/main/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstring.java)
350348
- 📄 [LongestValidParentheses](src/main/java/com/thealgorithms/dynamicprogramming/LongestValidParentheses.java)
351349
- 📄 [MatrixChainMultiplication](src/main/java/com/thealgorithms/dynamicprogramming/MatrixChainMultiplication.java)
352350
- 📄 [MatrixChainRecursiveTopDownMemoisation](src/main/java/com/thealgorithms/dynamicprogramming/MatrixChainRecursiveTopDownMemoisation.java)
@@ -406,7 +404,7 @@
406404
- 📄 [ActivitySelection](src/main/java/com/thealgorithms/greedyalgorithms/ActivitySelection.java)
407405
- 📄 [BandwidthAllocation](src/main/java/com/thealgorithms/greedyalgorithms/BandwidthAllocation.java)
408406
- 📄 [BinaryAddition](src/main/java/com/thealgorithms/greedyalgorithms/BinaryAddition.java)
409-
- 📄 [CoinChange](src/main/java/com/thealgorithms/greedyalgorithms/CoinChange.java)
407+
- 📄 [GreedyCoinChange](src/main/java/com/thealgorithms/greedyalgorithms/GreedyCoinChange.java)
410408
- 📄 [DigitSeparation](src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparation.java)
411409
- 📄 [EgyptianFraction](src/main/java/com/thealgorithms/greedyalgorithms/EgyptianFraction.java)
412410
- 📄 [FractionalKnapsack](src/main/java/com/thealgorithms/greedyalgorithms/FractionalKnapsack.java)
@@ -595,7 +593,7 @@
595593
- 📄 [ArrayRightRotation](src/main/java/com/thealgorithms/others/ArrayRightRotation.java)
596594
- 📄 [BFPRT](src/main/java/com/thealgorithms/others/BFPRT.java)
597595
- 📄 [BankersAlgorithm](src/main/java/com/thealgorithms/others/BankersAlgorithm.java)
598-
- 📄 [BoyerMoore](src/main/java/com/thealgorithms/others/BoyerMoore.java)
596+
- 📄 [BoyerMooreMajorityVote](src/main/java/com/thealgorithms/others/BoyerMooreMajorityVote.java)
599597
- 📄 [BrianKernighanAlgorithm](src/main/java/com/thealgorithms/others/BrianKernighanAlgorithm.java)
600598
- 📄 [CRC16](src/main/java/com/thealgorithms/others/CRC16.java)
601599
- 📄 [CRC32](src/main/java/com/thealgorithms/others/CRC32.java)
@@ -627,7 +625,6 @@
627625
- 📄 [TwoPointers](src/main/java/com/thealgorithms/others/TwoPointers.java)
628626
- 📄 [Verhoeff](src/main/java/com/thealgorithms/others/Verhoeff.java)
629627
- 📁 **cn**
630-
- 📄 [HammingDistance](src/main/java/com/thealgorithms/others/cn/HammingDistance.java)
631628
- 📁 **physics**
632629
- 📄 [CoulombsLaw](src/main/java/com/thealgorithms/physics/CoulombsLaw.java)
633630
- 📄 [DampedOscillator](src/main/java/com/thealgorithms/physics/DampedOscillator.java)
@@ -834,7 +831,6 @@
834831
- 📄 [StringMatchFiniteAutomata](src/main/java/com/thealgorithms/strings/StringMatchFiniteAutomata.java)
835832
- 📄 [SuffixArray](src/main/java/com/thealgorithms/strings/SuffixArray.java)
836833
- 📄 [Upper](src/main/java/com/thealgorithms/strings/Upper.java)
837-
- 📄 [ValidParentheses](src/main/java/com/thealgorithms/strings/ValidParentheses.java)
838834
- 📄 [WordLadder](src/main/java/com/thealgorithms/strings/WordLadder.java)
839835
- 📄 [ZAlgorithm](src/main/java/com/thealgorithms/strings/ZAlgorithm.java)
840836
- 📁 **zigZagPattern**

src/main/java/com/thealgorithms/bitmanipulation/HammingDistance.java

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/main/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstring.java

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/main/java/com/thealgorithms/greedyalgorithms/CoinChange.java

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.thealgorithms.greedyalgorithms;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Comparator;
6+
7+
/**
8+
* Greedy coin change (renamed for clarity).
9+
*
10+
* <p>Resolves naming conflict: "CoinChange" exists in both dynamicprogramming (DP: count combinations,
11+
* minimum coins) and greedyalgorithms. This class is the greedy approach using fixed denominations.
12+
*
13+
* <p>Returns a list of coins that sum to the given amount using a greedy strategy with
14+
* standard denominations (1, 2, 5, 10, 20, 50, 100, 500, 2000).
15+
*
16+
* @see <a href="https://en.wikipedia.org/wiki/Change-making_problem">Change-making problem</a>
17+
*/
18+
public final class GreedyCoinChange {
19+
private GreedyCoinChange() {
20+
}
21+
22+
/**
23+
* Returns a list of coins (in descending order of use) that sum to the given amount.
24+
* Uses greedy selection with fixed denominations.
25+
*
26+
* @param amount the target amount
27+
* @return list of coin values that sum to amount (may be empty if amount is 0)
28+
*/
29+
public static ArrayList<Integer> coinChangeProblem(int amount) {
30+
Integer[] coins = {1, 2, 5, 10, 20, 50, 100, 500, 2000};
31+
Arrays.sort(coins, Comparator.reverseOrder());
32+
33+
ArrayList<Integer> ans = new ArrayList<>();
34+
35+
for (Integer coin : coins) {
36+
while (coin <= amount) {
37+
ans.add(coin);
38+
amount -= coin;
39+
}
40+
}
41+
return ans;
42+
}
43+
}

src/main/java/com/thealgorithms/others/BoyerMoore.java renamed to src/main/java/com/thealgorithms/others/BoyerMooreMajorityVote.java

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package com.thealgorithms.others;
2+
23
import java.util.Optional;
34

45
/**
5-
* Utility class implementing Boyer-Moore's Voting Algorithm to find the majority element
6-
* in an array. The majority element is defined as the element that appears more than n/2 times
7-
* in the array, where n is the length of the array.
6+
* Boyer-Moore Majority Vote Algorithm (renamed for clarity).
7+
*
8+
* <p>Resolves naming conflict: "BoyerMoore" in others referred to the majority vote algorithm,
9+
* while searches.BoyerMoore is the string-search algorithm. This class is the majority-vote implementation.
810
*
9-
* For more information on the algorithm, refer to:
10-
* https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm
11+
* <p>Finds the majority element in an array (element that appears more than n/2 times).
12+
*
13+
* @see <a href="https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm">Boyer–Moore majority vote algorithm</a>
1114
*/
12-
public final class BoyerMoore {
13-
private BoyerMoore() {
15+
public final class BoyerMooreMajorityVote {
16+
private BoyerMooreMajorityVote() {
1417
}
1518

1619
/**
@@ -33,12 +36,6 @@ public static Optional<Integer> findMajorityElement(int[] array) {
3336
return Optional.empty();
3437
}
3538

36-
/**
37-
* Identifies the potential majority candidate using Boyer-Moore's Voting Algorithm.
38-
*
39-
* @param array the input array
40-
* @return the candidate for the majority element
41-
*/
4239
private static int findCandidate(final int[] array) {
4340
int count = 0;
4441
int candidate = -1;
@@ -51,13 +48,6 @@ private static int findCandidate(final int[] array) {
5148
return candidate;
5249
}
5350

54-
/**
55-
* Counts the occurrences of the candidate element in the array.
56-
*
57-
* @param candidate the candidate element
58-
* @param array the input array
59-
* @return the number of times the candidate appears in the array
60-
*/
6151
private static int countOccurrences(final int candidate, final int[] array) {
6252
int count = 0;
6353
for (int value : array) {
@@ -68,13 +58,6 @@ private static int countOccurrences(final int candidate, final int[] array) {
6858
return count;
6959
}
7060

71-
/**
72-
* Determines if the count of the candidate element is more than n/2, where n is the length of the array.
73-
*
74-
* @param count the number of occurrences of the candidate
75-
* @param totalCount the total number of elements in the array
76-
* @return true if the candidate is the majority element, false otherwise
77-
*/
7861
private static boolean isMajority(int count, int totalCount) {
7962
return 2 * count > totalCount;
8063
}

src/main/java/com/thealgorithms/others/cn/HammingDistance.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)