Skip to content

Commit

Permalink
Oracle: Deduplicaiton of singly linked list
Browse files Browse the repository at this point in the history
Signed-off-by: yourarj <[email protected]>

 Date:      Fri Dec 29 08:17:02 2023 +0530

 On branch main

 Changes to be committed:
	new file:   .idea/misc.xml
	new file:   .idea/modules.xml
	new file:   .idea/rust-ds-algo.iml
	new file:   .idea/vcs.xml
	new file:   .idea/workspace.xml
	modified:   Cargo.lock
	modified:   Cargo.toml
	modified:   leetcode-two-sum/src/main.rs
	new file:   oracle-linked-list-unique-elements/Cargo.toml
	new file:   oracle-linked-list-unique-elements/LinkedListDeDuplication.md
	new file:   oracle-linked-list-unique-elements/src/lib.rs
  • Loading branch information
yourarj committed Dec 29, 2023
1 parent 1537847 commit 4693bf2
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/rust-ds-algo.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ members = [
"leetcode-remove-duplicates-from-sorted-array",
"leetcode-sqrtx",
"leetcode-find-the-index-of-the-first-occurrence-in-a-string",
"oracle-linked-list-unique-elements",
]
2 changes: 1 addition & 1 deletion leetcode-two-sum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut answer = Vec::new();
for (index, element) in nums.into_iter().enumerate() {
if complement_map_with_index.contains_key(&element) {
answer.push(*complement_map_with_index.get(&element).unwrap().deref());
answer.push(*complement_map_with_index.get(&element).unwrap());
answer.push(index as i32);
break;
}
Expand Down
8 changes: 8 additions & 0 deletions oracle-linked-list-unique-elements/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "oracle-linked-list-unique-elements"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
24 changes: 24 additions & 0 deletions oracle-linked-list-unique-elements/LinkedListDeDuplication.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Deduplication in Singly-Linked-List
## Asked by: Oracle

Remove all instances of element from singly linked list so the resulting linked list will only contain elements whose
occurrence was exactly once.

e.g.
Example 01
```
Input: 1 -> 1 -> 1 -> 2 -> 3 -> 3 -> 4
Output: 2 -> 4
```

Example 02
```
Input: 1 -> 1 -> 1
Output: null
```

Example 03
```
Input: 1 -> 1 -> 1 -> 2 -> 3 -> 3
Output: 2
```
61 changes: 61 additions & 0 deletions oracle-linked-list-unique-elements/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
pub fn deduplicate_linked_list(list: Vec<i32>) -> Vec<i32> {
let mut output = vec![];
for index in 0..list.len() {
let add_current = match (
if index == 0 {
None
} else {
list.get(index - 1)
},
list[index],
list.get(index + 1),
) {
(None, _, None) => true,
(None, current, Some(next)) => current != *next,
(Some(last), current, Some(next)) => *last != current && current != *next,
(Some(last), current, None) => *last != current,
};

if add_current {
output.push(list[index])
}
}
output
}

#[cfg(test)]
mod tests {
use crate::deduplicate_linked_list;

#[test]
fn test_deduplicate_linked_list() {
assert_eq!(
vec![2, 4],
deduplicate_linked_list(vec![1, 1, 1, 2, 3, 3, 4])
);
}

// test 1 -> 1 -> 1 -> 2 -> 3 -> 3 -> 4 -> 4
#[test]
fn test_deduplicate_linked_list2() {
assert_eq!(
vec![2],
deduplicate_linked_list(vec![1, 1, 1, 2, 3, 3, 4, 4])
);
}

#[test]
fn test_remove_no_elements_from_all_unique_linked_list() {
assert_eq!(vec![1, 2, 3, 4], deduplicate_linked_list(vec![1, 2, 3, 4]));
}

#[test]
fn test_do_nothing_on_empty_linked_list() {
assert!(deduplicate_linked_list(vec![]).is_empty());
}

#[test]
fn test_remove_everything_from_all_duplicated_linked_list() {
assert!(deduplicate_linked_list(vec![100, 100]).is_empty());
}
}

0 comments on commit 4693bf2

Please sign in to comment.