Skip to content

Commit a652e6a

Browse files
authored
Merge branch 'master' into qt
2 parents 473dff2 + 90d20b3 commit a652e6a

File tree

4 files changed

+80
-6
lines changed

4 files changed

+80
-6
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* Utility class to find the single non-duplicate element from an array
5+
* where all other elements appear twice.
6+
* <p>
7+
* The algorithm runs in O(n) time complexity and O(1) space complexity
8+
* using bitwise XOR.
9+
* </p>
10+
*
11+
* @author <a href="http://github.com/tuhinm2002">Tuhin M</a>
12+
*/
13+
public final class SingleElement {
14+
15+
/**
16+
* Private constructor to prevent instantiation of this utility class.
17+
* Throws an UnsupportedOperationException if attempted.
18+
*/
19+
private SingleElement() {
20+
throw new UnsupportedOperationException("Utility Class");
21+
}
22+
23+
/**
24+
* Finds the single non-duplicate element in an array where every other
25+
* element appears exactly twice. Uses bitwise XOR to achieve O(n) time
26+
* complexity and O(1) space complexity.
27+
*
28+
* @param arr the input array containing integers where every element
29+
* except one appears exactly twice
30+
* @return the single non-duplicate element
31+
*/
32+
public static int findSingleElement(int[] arr) {
33+
int ele = 0;
34+
for (int i = 0; i < arr.length; i++) {
35+
ele ^= arr[i];
36+
}
37+
return ele;
38+
}
39+
}

src/main/java/com/thealgorithms/searches/BM25InvertedIndex.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,6 @@ private double computeBM25Score(int termFrequency, double docLength, double idf)
215215
*/
216216
private double computeIDF(int docFrequency) {
217217
// Total number of documents in the index
218-
return Math.log((totalDocuments - docFrequency + 0.5) / (docFrequency + 0.5));
218+
return Math.log((totalDocuments - docFrequency + 0.5) / (docFrequency + 0.5) + 1);
219219
}
220220
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
public final class SingleElementTest {
11+
12+
/**
13+
* Parameterized test to find the single non-duplicate element
14+
* in the given arrays.
15+
*
16+
* @param arr the input array where every element appears twice except one
17+
* @param expected the expected single element result
18+
*/
19+
@ParameterizedTest
20+
@MethodSource("provideTestCases")
21+
void testFindSingleElement(int[] arr, int expected) {
22+
assertEquals(expected, SingleElement.findSingleElement(arr));
23+
}
24+
25+
/**
26+
* Provides test cases for the parameterized test.
27+
*
28+
* @return Stream of arguments consisting of arrays and expected results
29+
*/
30+
private static Stream<Arguments> provideTestCases() {
31+
return Stream.of(Arguments.of(new int[] {1, 1, 2, 2, 4, 4, 3}, 3), Arguments.of(new int[] {1, 2, 2, 3, 3}, 1), Arguments.of(new int[] {10}, 10));
32+
}
33+
}

src/test/java/com/thealgorithms/searches/BM25InvertedIndexTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@ void testSearchRanking() {
5050
// Perform search for the term "good"
5151
List<SearchResult> results = index.search("good");
5252
assertFalse(results.isEmpty());
53-
53+
for (SearchResult result : results) {
54+
System.out.println(result);
55+
}
5456
// Validate the ranking based on the provided relevance scores
55-
assertEquals(6, results.get(0).getDocId()); // It's a Wonderful Life should be ranked 1st
56-
assertEquals(7, results.get(1).getDocId()); // The Pursuit of Happyness should be ranked 2nd
57+
assertEquals(1, results.get(0).getDocId()); // The Shawshank Redemption should be ranked 1st
58+
assertEquals(8, results.get(1).getDocId()); // A Few Good Men should be ranked 2nd
5759
assertEquals(5, results.get(2).getDocId()); // Good Will Hunting should be ranked 3rd
58-
assertEquals(8, results.get(3).getDocId()); // A Few Good Men should be ranked 4th
59-
assertEquals(1, results.get(4).getDocId()); // The Shawshank Redemption should be ranked 5th
60+
assertEquals(7, results.get(3).getDocId()); // The Pursuit of Happyness should be ranked 4th
61+
assertEquals(6, results.get(4).getDocId()); // It's a Wonderful Life should be ranked 5th
6062

6163
// Ensure the relevance scores are in descending order
6264
for (int i = 0; i < results.size() - 1; i++) {

0 commit comments

Comments
 (0)